Skip Navigation

[Resolved] Notification to child post authors when parent post author edits the parent post

This thread is resolved. Here is a description of the problem and solution.

Problem:
The client has related posts whereby child posts may be authored by different users than the parent post. The child post authors should be notified whenever the parent post is edited.

Solution:
The parent post edit form needs a notification set up. The recipients of that notification can then be customised using the cred_notification_recipients filter: https://toolset.com/documentation/programmer-reference/cred-api/#cred_notification_recipients

The particulars of where to get the user email addresses from are specific to this case, but custom code for this is suggested below which would need adapting: https://toolset.com/forums/topic/notification-to-child-post-authors-when-parent-post-author-edits-the-parent-post/#post-612960

This support ticket is created 6 years, 2 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.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

Author
Posts
#608332

Hi there. Hoping you can advise.

I have a parent post ($event) and various child posts (favourite, story etc).

When a member creates an $event an email CF is generated from the CRED form with the parent author's email.

When another member creates any child post - from the $event post - the author of the $event receives a notification (using the 'to an email specified in a form field' option in the CRED form). The child post cred form also generates an email CF with the child post author's email.

Each time a new child post is created the author of the $event receives an email. Eventually there could be several child post authors which I could display in a VIEW etc. And this is all working nicely.

However, when another member creates a child post I would like so send notification to the other child post authors.

Do you think this is possible? I have tried using an unformatted view of the emails separated by a comma and space (, ) as the value='' in a 'single line' CF in the CRED form to create a new child post. But that just breaks...

So the question is 'How do I get the string of email addresses (of all the other child post authors) into the child post cred form?

Do you think it is possible?

Look forward to hearing from you!

#608548

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Rita

It sounds like you have access to the required email addresses and it is just a question of updating who receives the notifications.

For that you can use the cred_notification_recipients filter: https://toolset.com/documentation/programmer-reference/cred-api/#cred_notification_recipients

There is an example on that page, but if you need help let me know and I'll get more details from you.

#610153

Hi Nigel
I am so sorry, you over estimate me... I have the email recipients in a view but I don't know php... or how to apply this to the filter example.. Everything I guess just breaks... Can you help?
Rita

#610167

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Rita

I'll need some more details from you.

When a new author publishes a child post, these are registered users on the site, and so the user has a registered email address stored as user meta, yes?

So when the child post is published, I can see what the parent post is, get all of the child posts of that parent, get the authors of each of those posts, and from there generate the list of email addresses.

Does that sound right?

#610207

Yes, thats exactly right 🙂 To add to this, each child post has a child post. So for example we could have:

Parent Post 'story'
Child Post 'comment' - parent post is story
Child Post 'comment-reply' - parent post is comment

I have this same structure for a few different things on the site.

At the moment I am collating the email addresses by using toolset custom fields only. So for example when a registered user creates a story post the cred form has hidden custom fields collecting the user's name, email address, title of the parent post and any other info I want to collect for the 'cycle' of notifications. Then when another user creates a comment child post, that info is brought into the comment post using the CRED form with the custom fields. Each child post picks up its parent posts collected info. I just used the logic of Toolset. No php. This works perfectly fine though I realise it's probably not 'developer cool'.

Just using these custom fields I have created notifications that send emails to:
1. the parent story author when a child comment is created.
2. the child comment author when the parent story author creates a child comment-reply on the parent comment.
3. the child comment author when parent story author creates a child comment-reply on the parent comment.

Where I come to a screeching halt is the possibility that another user may enter the 'cycle' and create a child comment-reply on a parent comment. Then I suddenly have a 'string' of emails. I don't know how to get the cred form to send to two email addresses or three or however many other users enter the cycle by creating a comment-reply.

So I created another custom field (single-line) which collates all these 'raw' email addresses into a string seperated by commas thinking if I could just get them into the BCC of the notification form...

Maybe I should do things differently.. It all started because some users do not wish their username to be public so when they register they can create a profile and choose a 'username' which is visible on all their posts instead of the user's meta. Rather annoying but that's how I came thundering down the custom field road instead of using meta which would be much easier..

Does this make sense?

#610211

Equally, I just haven't had a chance to learn php and as my site develops I see I need to build on my knowledge... Appreciate your advice 🙂

#610760

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Rita

Does it make sense?

Sort of.

From what you've written I've understood that whenever a post is submitted with a CRED form there are fields (or one field with a comma-separated list) which have the relevant email addresses.

So you just need to use the cred_notification_recipients hook I linked to before: https://toolset.com/documentation/programmer-reference/cred-api/#cred_notification_recipients

I edited the example to demonstrate the kind of thing I think you will need:

/**
 * Customise CRED notification recipients by adding a BCC
 * to the the notification "Notification name"
 */
add_filter('cred_notification_recipients', 'tssupp_custom_recipients', 10, 4);
function tssupp_custom_recipients( $recipients, $notification, $form_id, $post_id ) {

    $which_notifications = array( 'Notification name' );

    $recipient_field = "wpcf-email-list";

    // Check notification name matches target notification
    if ( isset( $notification['name'] ) && in_array( $notification['name'], $which_notifications ) ) {

        // Get the email list field that stores recipients
        $recipient_list = get_post_meta( $post_id, $recipient_field, true );

        // Add recipients by BCC
        if ( isset( $recipient_list ) ) {
            $recipients[] = array(
                'to'        =>  'bcc',
                'address'   => $recipient_list,
                'name'      =>  '',
                'lastname'  =>  ''
            );            
        }
    }
    return $recipients;
}

Note you need to edit the $which_notifications array which should be a list of the names of the notifications you want this to apply to (which can be just one, or a comma-separated list), and also the name of the field which stores the list of email addresses (in my example a Types custom field with a slug of "email-list", which is stored in wp_postmeta with a key of "wpcf-email-list").

Is that enough to get you off on the right foot?

#612107

Hi Nigel

I am sorry 🙂 I have tried everything I can think of, guess, estimate, have found in toolset documentation etc etc. I either get a white screen or no response.

The NOTIFICATION NAME on the parent post cred edit form is 'to favourites list' or is it 'to-favourites-list' ??

I have the list of emails collated in a view (view ID 41237) not a custom field. It is basically all the child post author's email addresses. The view has a filter - to list ony posts that are children of the page where this view is shown and display the email addresses in RAW format separated by a comma and space. Example john@gmail.com, peter@gmail.com,

You have used a custom field in your example...

So how would I collate these email addresses into a custom field? or how would I get the view results into the php above?

I tried all these ideas to: https://toolset.com/documentation/programmer-reference/views-api/

I am sorry... I just can't figure it out... 🙁

#612210

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Rita

In your previous reply you said you had created a single line custom field that was a comma-separated list of the target email addresses, that's what I was expecting you to use with the code I suggested, but in your last update you say you are generating the list with a View, so I'm not sure what to base this on.

In any case, your View won't help you in the code you need to use with the cred_notification_recipients filter, you will effectively need to recreate the View in PHP.

So, the form for the notification in question, it is publishing a post, which has a parent, and you want to add notifications to the authors of the sibling posts, yes? That is, to other posts which have the same parent post, is that correct?

So the form knows the ID of the post it is itself publishing. And from that post we can get the ID of the parent post. We can then create a custom query a bit like your View which gets all of the child posts of that parent.

Can we extract the email addresses from those? Is it the email address of a registered user that is author of the post? Or do I get the email address from a custom field?

By whatever means I can build the list of email addresses, and then add them as BCC: recipients to this notification.

That's how I envisage it working, is that correct?

#612228

Hi Nigel

Yes, sorry I see that I wasn't clear... My fault!

I established a collection of custom fields in every cred form for both parent and child posts to collect all these email addresses (and public usernames etc) and send notifications. Which works great except when I want to send to more than one email address.

Example, when a registered user creates any child post (favourite, story, comment etc) on that parent event post, a notification will go to the 'wpcf-notification-parent-author-email' that was set when the registered user created the event post. And so on.

So in answer to your questions:

So, the form for the notification in question, it is publishing a post, which has a parent, and you want to add notifications to the authors of the sibling posts, yes? That is, to other posts which have the same parent post, is that correct? = Yes. To other sibling child posts of the same post type which have the same parent.

Can we extract the email addresses from those? Is it the email address of a registered user that is author of the post? Or do I get the email address from a custom field? = Yes, all the users are registered. I have been setting their email addresses in a custom field to use in all the other notifications so we can either use the custom field or 'user email' as you prefer.

🙂

#612960

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Rita

OK, on the basis of what you've said, let me propose the following

Note that you will need to edit the initial lines for post type slugs etc., and please understand that providing custom code is outside of our support policy. I have written the following without testing and believe it should work, but you may find you need to modify it to suit your needs. Be sure to read through the code in case I have missed something.

/**
 * Customise CRED notification recipients by adding a BCC
 * to the the notification "Notification name"
 */
add_filter('cred_notification_recipients', 'tssupp_custom_recipients', 10, 4);
function tssupp_custom_recipients( $recipients, $notification, $form_id, $post_id ) {
 
	// Edit these as required
    $which_notifications = array( 'Notify sibling' ); // which notifications this applies to
    $parent_slug = 'parent-slug'; // parent post type slug
    $child_slug = 'child-slug'; // child post type slug
 
    // Check notification name matches target notification
    if ( isset( $notification['name'] ) && in_array( $notification['name'], $which_notifications ) ) {
 
    	// Get the parent id of this post
    	$parent_id = get_post_meta( $post_id, 'wpcf_belongs_' . $parent_slug . '_id', true );

    	// Get the child posts of that parent, excluding this post (i.e. the siblings)
    	$args = array(
    		'post_type'		=>	$child_slug,
    		'numberposts'	=>	-1,
    		'post__not_in'	=>	$post_id,
    		'meta_key'		=>	'wpcf_belongs_' . $parent_slug . '_id',
    		'meta_value'	=>	$parent_id
    	);
    	$child_posts = get_posts( $args );

    	// Start with an empty list of email addresses and empty list of post author IDs
    	$emails = "";
    	$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 ) . ',';
    	}
 
        // Add recipients by BCC
        if ( $emails != '' ) {
            $recipients[] = array(
                'to'        =>  'bcc',
                'address'   => $emails,
                'name'      =>  '',
                'lastname'  =>  ''
            );            
        }
    }
    return $recipients;
}
This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.