Skip Navigation

[Resolved] CRED form notification not showing parent post fields

This support ticket is created 4 years 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)

This topic contains 8 replies, has 3 voices.

Last updated by Ian Henderson 4 years ago.

Assisted by: Nigel.

Author
Posts
#1834501

I have two notifications on a CRED post form - one triggered on completion of the form, and one on publishing the post. Each should display fields from the post in question, and its parent.

The notification on submitting the form works perfectly:

Hi [types field='first-name' item="%%POST_ID%%"][/types]

Thank you for registering for this event:

EVENT INFO
Title: [wpv-post-link item="@event-participant.parent"]
Date: [types field='start-date' style='text' format='l, j F Y' item='@event-participant.parent'][/types]
Time: [types field='start-date' style='text' format='G:i T' item='@event-participant.parent'][/types]

The second notification is triggered when the post created with the above form is published - it is exactly the same, except for altered text and one additional field:

Hi [types field='first-name' item="%%POST_ID%%"][/types]

Please find final information for your event:

EVENT INFO
Title: [wpv-post-link item="@event-participant.parent"]
Date: [types field='start-date' style='text' format='l, j F Y' item='@event-participant.parent'][/types]
Time: [types field='start-date' style='text' format='G:i T' item='@event-participant.parent'][/types]

Event link: [types field='event-url' item="@event-participant.parent"][/types]

However, this notification arrives with the correct name (inserting the data from the field in the post) but the parent data is all blank/missing.

Any idea what I'm doing wrong?

thanks
Ian

#1835225

Hello,

I suggest you try these:
1) Create a content template "parent-post-info", put all parent post information into this content template.
2) In the notification mail content, display above content template shortcode with item attribute, for example:
[wpv-post-pody view_template="parent-post-info" item="@event-participant.parent"]

More help:
https://toolset.com/documentation/programmer-reference/views/views-shortcodes/#wpv-post-body
https://toolset.com/documentation/user-guides/views-shortcodes/item-attribute/

#1835313

Hi Luo

I have tried this and it just came up blank - didn't even show the text in the content template. I have not assigned that content template to a post type because then it will break the display of the parent posts (events) in the front end.

Can you explain why the notification emails are working 100% for displaying parent post fields in the form submission trigger but not the post publish trigger, when they are the same content?

best
Ian

#1836177

I have tried it in my localhost with a fresh WP installation + the latest version of Toolset plugins, it works fine, I can see the parent post information in the email notification.

So the problem you mentioned above is abnormal, please check these:
1) Make sure you are using correct relationship slug "event-participant"
2) Make sure you are using the latest version of Toolset plugins, you can download them here:
https://toolset.com/account/downloads/
3) In case it is a compatibility problem, please deactivate all other plugins, and switch to wordpress default theme 2020, deactivate all custom PHP codes, and test again
4) Also check if there is any PHP/JS error in your website:
https://toolset.com/documentation/programmer-reference/debugging-sites-built-with-toolset/
5) If the problem still persists, please provide database dump file(ZIP file) of your website, you can put the package files in your own google drive disk, share the link only, also point out the problem page URL and form URL, I need to test and debug it in my localhost, thanks
https://toolset.com/faq/provide-supporters-copy-site/

#1839681

Hi Luo

I have created a basic test site to re-test this, and can confirm the bug:

This works fine as long as there is one single parent post (event). As soon as you create a second event, then the notifications break on the publish trigger. You should be able to verify this in your test installation without even installing mine.

If you delete all the parent posts except for one, then the notifications start working again.

To confirm - the post form trigger still works with more than one parent post published, but the publish trigger doesn't.

regards
Ian

#1841381

Nigel
Supporter

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

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

Hi Ian

Sorry for the delay getting back to you, Luo is on vacation and we have a couple of unexpected absences so that we are a little stretched.

I'm taking this into my queue and will handle it.

I've read through the details, I have a suspicion about what the cause may be, but I need to create my own test site to verify.

I'll do that and get back to you.

Thanks for your patience.

#1841387

Thanks Nigel - much appreciated!

Should be very straightforward to recreate the error. With a single parent post it works perfectly, add a second post and the 'on publish' notification will be blank in any reference to relationship post.

#1842125

Nigel
Supporter

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

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

Hi Ian

I did some thorough testing on a clean site, which led me to find and review some existing internal tickets relating to notifications and fields from related posts.

Firstly, something that confused me was that the placeholders for the title and link to the parent post don't work, but it turns out that's known and expected: https://toolset.com/errata/post_parent_title-placeholder-doesnt-work-in-cred-notification-emails/

It's still possible to choose them in the UI for legacy reasons, but they don't work.

The correct way to include fields from related posts (e.g. parents) is to use the item attribute to specify a different source for the shortcodes than the current post.

Now the problem.

At the time the form is submitted the current post context is correct, it's the post that was just created by the form.

But for other, deferred, notifications (e.g. when the post status changes), the context is lost, and you need to force it by adding the attribute item="%%POST_ID%%" to the shortcodes.

That's fine when you are outputting fields from that post. But you can't add the item attribute twice, once to set the current post context, and again to set the related post.

So the long and the short of it is that you cannot reference fields from related posts in deferred notifications.

(In principle it would be possible, just not with the current implementation in Forms. We have internal tickets about improving this, but it is a question of priorities.)

Now, if you really need this to work, you will need to come up with your own solution.

Shortcodes in the notifications will be parsed (not just Toolset ones).

So you could register a custom shortcode where you pass the post ID via an attribute using %%POST_ID%% to set the starting context, and from there, within your shortcode, you can use toolset_get_related_post to retrieve the parent post, and then return a string including whatever field is needed. (You could make several specific shortcodes, or you could make a more generic one and pass the relationship slug and the field slug as shortcode attributes, too.)

This simple example demonstrates that you can access the ID of the post created with the form, even in deferred notifications:

add_shortcode('related', function ($atts = []) {

    // provide defaults
    $atts = shortcode_atts(
        array(
            'id' => null,
        ),
        $atts
    );

    $output = "";

    if (isset($atts['id'])) {
		
		// logic goes here to get fields from related post
		// here I'm just returning the ID of the current post itself
        $output = $atts['id'];
    }

    return $output;
});

where you would add the following to the notification body:

[related id="%%POST_ID%%"]

See https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_post for getting the ID of the parent post

#1843285

Thanks very much Nigel! This sorted it for me. I used toolset_get_related_post to generate the parent ID within that shortcode and then I could call the correct post using the content template.

Many thanks
Ian