Skip Navigation

[Resolved] Form notification to email from parent post field no longer working after Types 3.0 update

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

Problem: I have a Form that triggers an email notification. I send the notification to an email address that is stored in a parent post's custom field, by using the CRED API cred_notification_recipients. Since updating to Types 3.0, the email notifications are not sent as expected.

Solution: Update the code to work with the new Post Relationships API, and ensure the child post status is not "private".

add_filter('cred_notification_recipients', 'modify_recipients', 10, 4);
      
function modify_recipients($recipients, $notification, $form_id, $post_id) {
    // Check notification name matches target notification
    if ( isset($notification['name']) && 'Mail an Veranstalter' == $notification['name'] ) {
        // Add email recipient
        $parent_id = toolset_get_parent_post_by_type( $post_id, 'veranstaltung' );
        $email_of_organizer = get_post_meta($parent_id, 'wpcf-veranstalter-email', true);
        $recipients[] = array(
            'to'        =>  'cc',
            'address'   => '$email_of_organizer',
            'name'      =>  '',
            'lastname'  =>  ''
            );
    }
    return $recipients;
}

Relevant Documentation:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_parent_post_by_type

This support ticket is created 6 years, 6 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 4 replies, has 2 voices.

Last updated by thomasD-2 6 years, 6 months ago.

Assisted by: Christian Cox.

Author
Posts
#914833

Hi Folks,
this hack that we implemented a few months ago doesn't work anymore after Updating to Types 3.0 and migration of the post relationships. It's likely due to the '_wpcf_belongs_event_id' in the code

Do you have a suggestion how to fix this?
Thx.
Best
Thomas

#914837

For reference from the previous ticket:

add_filter('cred_notification_recipients', 'modify_recipients', 10, 4);
     
function modify_recipients($recipients, $notification, $form_id, $post_id) {
    // Check notification name matches target notification
    if ( isset($notification['name']) && 'Mail an Veranstalter' == $notification['name'] ) {
        // Add email recipient
        $parent_id = get_post_meta($post_id, '_wpcf_belongs_veranstaltung_id', true);
        $email_of_organizer = get_post_meta($parent_id, 'wpcf-veranstalter-email', true);
        $recipients[] = array(
            'to'        =>  'cc',
            'address'   => '$email_of_organizer',
            'name'      =>  '',
            'lastname'  =>  ''
            );
    }
    return $recipients;
}

The main problem I see now in this code is the $parent_id variable. We should replace this line, using the new Post Relationships API to determine the correct parent post ID. Since this is a legacy relationship that has been migrated, the toolset_get_parent_post_by_type API is appropriate:

...
// Add email recipient
$parent_id = toolset_get_parent_post_by_type( $post_id, 'veranstaltung' );
...

New API documentation here: https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_parent_post_by_type

#915305

Thx for the quick response. I changed the line of code in the functions.php, but the email has not been sent.
Please advide how to follow up on this.
Best
Thomas

#915424

Can you tell me:
- What is the post status of the child and parent posts that are involved in your test?
- Are you using Types 3.0 or Types 3.0.1?

There was a known issue in Types 3.0 where the API had trouble retrieving information from related posts when the child posts are not "published". Types 3.0.1 includes a fix for this issue.

#915461

Hey Christian,
I'm using Types 3.0.1, and the post status of the new child post is set to "private".
The latter seems to be the problem. After switching this setting to "Draft" or "Public" the emails are sent correctly.

So this problem is solved now. Thx.