Skip Navigation

[Resolved] I need to send alert if a custom field value change compared to previous

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

Problem: I would like to trigger a notification conditionally based on the custom field values submitted in the User's current post and the User's previous post.

Solution: Use the PHP APIs to set up custom conditional notification triggers.

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_notification_event_type
https://toolset.com/documentation/programmer-reference/cred-api/#cred_custom_notification_event_type_condition
https://toolset.com/forums/topic/cred-form-notification-email-conditional-on-generic-field/

This support ticket is created 3 years, 8 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 2 replies, has 2 voices.

Last updated by claudioM-3 3 years, 8 months ago.

Assisted by: Christian Cox.

Author
Posts
#2035321

I have a post type with a custom field number and a custom data field. Each user can add his daily number in a new post by cred. I wish to send an email if the value number change (for example more than 5% ) compared to previous (data field) user's post_type.

#2035709

Hello, it sounds like you need a way to compare the custom field value from a Form submission to another custom field value from a previous submission, to determine whether or not an email notification should be sent. Forms email notifications do not include any built-in triggers exactly like what you have described, so I think the only solution would be to use custom code to create your own conditional notification trigger. We offer two PHP APIs you can use that help you set up your own custom notification conditional triggers:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_notification_event_type
https://toolset.com/documentation/programmer-reference/cred-api/#cred_custom_notification_event_type_condition

Nigel provided examples using these APIs to send notifications conditionally in another ticket here:
https://toolset.com/forums/topic/cred-form-notification-email-conditional-on-generic-field/

You'll have access to the new post ID in those API hook callbacks in the $post_id parameter, so you can get Toolset custom field values from that post using get_post_meta:

$field_slug = 'your-field-slug'; // change this to match your custom field slug from wp-admin
$value = get_post_meta( $post_id, 'wpcf-' . $field_slug, true);

To get the custom field value from a previous post submission, you'll need to query posts somehow and get the post ID of the second most recent submission. For example, a View of posts filtered by post author, where the post author is the same as the author of $post_id, sorted by post date in descending order. Offset the results by 1 to skip the most recent post, and get the next post with a limit of 1 result.
The get_view_query_results API would give you access to the results of such a View, i.e. the User's previous post submission. With that post ID, you can use get_post_meta again to get the custom field value from the previous post, then you could programmatically compare the two custom field values to conditionally trigger or not trigger the notification.

That Views PHP API is documented here:
https://toolset.com/documentation/programmer-reference/views-api/#get_view_query_results

You can see it's a fairly complex scenario that will require developer-level understanding of PHP, Toolset APIs, and WordPress APIs in general. If you're not comfortable writing PHP, you may need to enlist the assistance of an independent contractor to achieve exactly what you're looking for. If you run into problems, I can help troubleshoot any Toolset APIs that don't seem to be working as expected.

#2035711

My issue is resolved now. Thank you Christian !