This thread is resolved. Here is a description of the problem and solution.
Problem:
The issue here is that the user wanted his users to be able to subscribe to a notification type. Lets say the user has a Job type and wants to notify user when they subscribe to a notification of a particular job type.
Solution:
This can be done by creating a secondary CPT for the notifications where the user can create the notification type they want for a job.
So on this notification cpt it should have attributes from the Jobs CPT that can be used to match a notification for a Job type. The notification CPT must also have an email field.
Then you can use this code below to send an email to a notification post based on a matching custom field criteria.
Are you going to send the notification based on 2 fields or are you going to use a Single checkbox field on both posts ? Remember we did a single field check for the $job_type and notification_type.
With that the notification can only be one type. I though using the custom checkbox field would allow the user to create a single notification to notify them of the various items that were posted.
Hey Shane,
I am so sorry for confusing you.
I want to replicate the same form like in the jobs post type - for the job alarm.
That means the job alarm has multiple fields.
Select fields (single select) and multi checkboxes.
The function works fine till now. But the Email should be triggered when the single select fields exactly match and when one or mor things of the multi checkbox is matching.
hey shane, sorry you are right the alarm is working. just found out that an email is not sent out when you create the jobs in the back end. when the job is created in the front end emails are sending. Thank you.
just few question we still haveabout the function:
1. can i style the outgoing email in any way (html)?
2. can i bring the titel of the job alarm in the mail? So a user knows which alarm is triggered?
3. about the code. it is nearly perfect. but one thing was maybe not explained correctly from my site.
all fields have to match before sending an email out.
in our example:
we have till now two fields (job_type and job_stelle)
so the code should proof if the job_type is matching (yes-> check the next field (job_stelle*) if matching) (no-> do not check the next field and end the function).
*The multiple fields (job_stelle) are working great and this is correct to check if at least one thing is matching, so the function can go on to check the next field if one thing is matching.
at this time the function does send out an mail everytime if only one single item is matching. but the better way for the end user is to get notified only when all their attributes are matching.
The function should check one field after another and if only one field is not matching the function should stop beeing executed.
//Job Alarm function
add_action('cred_submit_complete', 'my_success_action',10,2);
function my_success_action($post_id, $form_data) {
if ( $form_data['id'] == 67) {
$job_type = get_post_meta($post_id, 'wpcf-position-normal',true);
$job_stelle = get_post_meta($post_id, 'wpcf-stelle-ist-geeignet-fur',true);
$notifications = get_posts('post_type=job-alarm');
foreach ($notifications as $notification) {
$email = get_post_meta($notification->ID, 'wpcf-e-mail', true);
$notification_type = get_post_meta($notification->ID, 'wpcf-position-alarm',true);
$notification_stelle = get_post_meta($notification->ID, 'wpcf-stelle-ist-geeignet-fur-alarm',true);
;
if ( $job_type == $notification_type && $job_stelle == $notification_stelle ) {
wp_mail($email, 'Dein Job Alarm: '.get_the_title($post_id), get_permalink($post_id)."HTML CODE CAN GO HERE");
}
}
}
}
I've adjusted the code above. You should now get the post title as well as the notification should only send when all the fields are the same as the jobs.
I was under the impression that at least one field has to match.
hey shane, oh man how can i explain more in detail how the alarm should behave.
first of of all the code is working fine. But i did modified it. because the multiselect field (notification_stelle) needs to have at least one attribute like in the multiselect from the job (job_stelle).
but the code count(array_intersect($job_stelle, $notification_stelle)) > 0 ) does only check if one random attribute (it doesn´t matter if there is a match or not) is selected.
here is the code i modified:
//Job Alarm function
add_action('cred_submit_complete', 'my_success_action',10,2);
function my_success_action($post_id, $form_data) {
if ( $form_data['id'] == 67) {
$job_type = get_post_meta($post_id, 'wpcf-position-normal',true);
$job_stelle = get_post_meta($post_id, 'wpcf-stelle-ist-geeignet-fur',true);
$notifications = get_posts('post_type=job-alarm');
foreach ($notifications as $notification) {
$email = get_post_meta($notification->ID, 'wpcf-e-mail', true);
$notification_type = get_post_meta($notification->ID, 'wpcf-position-alarm',true);
$notification_stelle = get_post_meta($notification->ID, 'wpcf-stelle-ist-geeignet-fur-alarm',true);
;
if ( $job_type == $notification_type && count(array_intersect($job_stelle, $notification_stelle)) > 0 ) { //this must check that at least one attribute of the multiselect is matching to the job_stelle but it only checks if one is selected
wp_mail($email, 'Dein Job Alarm: '.get_the_title($post_id), get_permalink($post_id)."HTML CODE CAN GO HERE
<h1>get_permalink($post_id</h1>");
}
}
}
}
The function should check one field after another but if only one field is not matching the function should stop being executed.
When the fields do match check the next field.
So the problem is only the multi select field. it has to match at least one attribute to the job multi select attributes.
yes i know shane but the function does not proof if only one multiselect attribute is matching.
it proofs that all multi select fields has to exactly match.
e.g. so the job has 5 multi select attributes (A,B,C,D,E) now in the function proofs that the job alarm has always to have A,B,C,D,E selected to trigger the mail.
But i need for the multi select to trigger the mail when at least one attribute matches.
so the alarm has only A,C,E and the mail should trigger. 3 attributes are matching to the job
so the alarm has only A the mail should trigger. 1 attribute is matching to the job
so the alarm has only F,G the should not trigger. 0 attributes
This is what I had it to originally. What this statement above means is that it will only trigger if job type and notification type is the same and at least 1 of the notification stelle matches the job stelle.