Hi, I've made a test on a new server.
This is the recap.
Posts type:
- Post type 1: Ricambisti
- Post type 2: Case automobilistiche
- Post type 3: Contatti generici ricambi
Relationship between post type 1 and post type 2: many to many (post type 1 is parent, post type 2 is child):
- slug: 'ricambista-casa-automobilistica'
Relationship between post type 2 and post type 3: one to many (post type 2 is parent, post type 3 is child).
- slug: 'marca-contatto-generico-ricambio'
Now I have a post form that creates a new post (a new post of the post type 3) where I have a field for selecting the parent (post type 2) for the post being created.
I want to send the notification email to all the authors of the parents (post type 1) of the parent (post type 2) when a new post is created.
I'll trigger the function only when the notification's name is 'Notifica ricambisti'
add_filter('cred_notification_recipients', 'ts_cred_notification_recipient_19', 10, 4);
function ts_cred_notification_recipient_19($recipients, $notification, $form_id, $post_id){
$which_notifications = array( 'Notifica ricambisti' ); // which notifications this applies to
// Check notification name matches target notification
if ( isset( $notification['name'] ) && in_array( $notification['name'], $which_notifications ) ) {
I'll get the ID of the parent post (case automobilistiche) from the relationship between post type 2 and post type 3
$case_automobilistiche_id = toolset_get_related_post($post_id, 'marca-contatto-generico-ricambio' );
I'll extract the post objects from the relationship between post type 1 and post type 2
$ricambisti = toolset_get_related_posts(
$case_automobilistiche_id,
'ricambista-casa-automobilistica',
'child',
100000,
0,
array(),
'post_object',
'parent'
);
And extract the authors email of the post type 1 related to the post type 2 which is related to the post type 3
// Start with an empty list of email addresses and empty list of post author IDs
$emails = array();
$authors = array();
// Loop over each parent post and get the post authors
foreach ($ricambisti as $ricambista) {
$authors[] = get_post_field ('post_author', $ricambista);
}
// Ignore duplicates
$authors = array_unique( $authors );
// Get email address of authors and create comma separated list
foreach ($authors as $author) {
$emails[] = get_the_author_meta( "user_email", $author );
}
//error_log(print_r($emails, true));
// Add recipients by BCC
if (!empty($emails)) {
$recipients[] = array(
'to' => 'bcc',
'address' => join(",",$emails),
'name' => '',
'lastname' => ''
);
}
//error_log("recipients---".print_r($recipients, true));
}
return $recipients;
}
If you want I can share with you FTP and admin credentials, so you can check.