Hi, I have a many to many relationship between two post types: companies and attributes.
A company could have many attributes and an attribute could belong to many companies.
I've created a view that displays all the companies for a specific attribute (every attribute has a page).
I've create another post type called message and place a message post form in each single attribute page (where the companies that have that attribute are shown through the view).
Now, I'd like to send an email notification to all the companies (to the authors email) that have a specific attribute, when a new message post are created with the message post form displayed on the attribute page.
Hello. Thank you for contacting the Toolset support.
Toolset Form's offers hook called cred_notification_recipients to add as many notification recipients as you want once you have all post author email address for your post type companies.
You should try to get company post author emails based on the current attribute ID and try to find all related company posts using the Toolset post-relationship API function: toolset_get_related_post
add_filter('cred_notification_recipients', 'modify_recipients', 10, 4);
function modify_recipients($recipients, $notification, $form_id, $post_id) {
if (11040== $form_id){ // here I placed my form ID
$parent_post_id = $post_id;
$relationship_slug = 'relationship-slug'; // here I placed the original relationship slug
$child_posts = toolset_get_related_posts(
$parent_post_id,
$relationship_slug,
'parent',
1000000,
0,
array(),
'post_object',
'child'
);
// Start with an empty list of email addresses and empty list of post author IDs
$emails = array();
$authors = array();
// Loop over each child post and get the post authors
foreach ($child_posts as $child_post) {
$authors[] = $child_post->post_author;
}
// 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;
}
I'm not sure if the code here is right, because I'm on the child post and I'd like to send the notification email to its parents.
I will require access details to know your post-relationship slug and please share the link of the page where I can see your form.
*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.
I have set the next reply to private which means only you and I have access to it.
So, as I understand, I can see the form with this link: hidden link
And once I click on submit button, it should send the email to post author's of the post that is listed just besides the form (Sicily and test) - correct? If yes:
- Can you please share where you added the cred_notification_recipients hook code?
Yes, exactly what I need 🙂
I've added the cred_notification_recipients hook code on the file functions.php here: /wp-content/themes/Avada-Child-Theme
Can you please use above code and make sure that when you submit the form the above hook should be triggeted and it should print "Hook cred_notification_recipients successfully triggeted" on the page.
Can you please try to add the following code - add the following code to functions.php file and remove all other cred_notification_recipients hooks.
function cred_notification_recipients_timestamp( $arg ){
error_log("cred_notification_recipients: this is triggered successfully at time " . microtime() );
return $arg;
}
add_filter( 'cred_notification_recipients', 'cred_notification_recipients_timestamp',10,4 );
Do you see the error log is printed with your error log file?
The first thing is that we need to make sure that the hook "cred_notification_recipients" is triggered when you submit the form.
If this does not work, I suggest you check on another test server without SMTP plugin and check email is working or not.
If that does not help, I need to create a test site to see if there is issue from our plugin.