Skip Navigation

[Resolved] Email notification to parents post authors on many to many relationship

This support ticket is created 4 years, 5 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 30 replies, has 2 voices.

Last updated by Minesh 4 years, 5 months ago.

Assisted by: Minesh.

Author
Posts
#1662985

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.

#1663001

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

The main thing I would like to know is - do you see the hook "cred_notification_recipients" is triggered when you submit the form?

#1664311

No, it isn't triggered.
But only with this function..

#1666663

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I just tested with my test site and added the following hook to custom code section:


function tssupp_custom_form_recipients1( $recipients, $notification, $form_id, $post_id ){
  
error_log("MInesh:  cred_notification_recipients: this is triggered successfully at time " . date("d-m-Y H:i:s") );
  
  
   $recipients[] = array(
                'to' => 'to',
                'address' => "xxx@gmail.com",
                'name' => '',
                'lastname' => ''
                );
  
  
    return $recipients;
}
add_filter( 'cred_notification_recipients', 'tssupp_custom_form_recipients1', 10, 4);

And I can confirm that when I submit the form I can see the line added to error log:

"MInesh:  cred_notification_recipients: this is triggered successfully at time " . date("d-m-Y H:i:s")

And also the notification send to xxx email.

If you would like to check I can share the access details to my test site.

Then, I've added the same code I tried with my test site to your site's custom code section and also activated the debug but when I submit the form I do not see any error log entry added to error log file nor the email sent.

This certainly looks like issue from server. Do you have any other test site hosted on another server where I can run a test?

#1666891

Ok, could you please share the access details to your test site.
Thank you.

#1666893

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Sure.
URL: hidden link
user: demo
pwd: Admin@999

The form is available here:
=> hidden link

You can see the notification settings and edit the form here:
=> hidden link

You can find the hook added with the following link and you can change the email to your desired email:
=> hidden link

Let me know if you need anything.

#1666991

Is there a way to have toolset in English and not translated in Italian?

#1667033

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Do you mean in admin you want to display Toolset menu in English - if yes, what if you try to change your site language from Settings => General => Site Language and chose your site language.

#1667199

Hi, I've tested on a new server.
The email is sent and the error is printed on the error_log file.
At this point, I think I'm not correctly triggering the function to send the email notification to the authors of the parent post.

#1667835

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Great - so finally we have a place where we can add the correct code.

Can you please send me access details of your server where the notification works. As well as share the page where you added the form.

I have set the next reply to private which means only you and I have access to it.

#1668007

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

When I edit the page where you added the form: hidden link

I can see the two post relationship available - to which post relationship post author you want to send the email, Can you please clarify and once you share this information I will adjust to notification hook code accordingly.
=> hidden link

Also, I see you added two code snippet for notification hook, which I should use and adjust the code?

#1668133

I'd like to send the email to the author of the post type "Ricambisti".

The hook is this one: notifica-ai-ricambisti

#1668163

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

It works as expected I can see now.

I've added the new user test-notification-user1 and test-notification-user2.

Then, I've changed the post author of the following related posts:
=> hidden link - author: test-notification-user1
=> hidden link - author: test-notification-user2

And then submit the form:
=> hidden link

I received the notification to both post author's email for the author "test-notification-user1" and "test-notification-user2".

Can you please confirm now it works as your end, you should check spam folder as well for email. You can change the post author for the post and test it.

#1668167

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I've also set the my email address to section "Send notification to a specific email address: " to the notification "Notifica ricambisti", you should also change the email there as well.
=> hidden link

#1668171

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Please remove the users I've added test-notification-user1 and test-notification-user2 once the testing is complete. 🙂