Skip Navigation

[Resolved] How to send a notification before a post expires?

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

Problem:
A CRED form publishes posts with an expiry period. How to send a notification to users at some interval before the post expires?

Solution:
There is an option in the notifications to send a notification x minutes before the post expires.

Relevant Documentation:
https://toolset.com/documentation/user-guides/automated-email-notifications-with-cred/

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

This topic contains 19 replies, has 4 voices.

Last updated by Nigel 6 years, 10 months ago.

Assisted by: Nigel.

Author
Posts
#598260

Hi,

I have a CRED form with email/date fields and when submitted it creates a cpt (Reminder Email). I found the code below in another toolset post and added it to my functions.php which sets the post expiration date to the date the user has entered, and it sets the post to published:

//add post expiration date base on user date

add_action('cred_submit_complete', 'expiration_update',10,2);
function expiration_update($post_id, $form_data)
{
  
    // if a specific form
    if ($form_data['id']=='1844')
    {
    //We get our new date value coming from our Date Custom Field
    //We need a single value, not an array, so it's set to TRUE
     $new_date = get_post_meta($post_id, 'wpcf-reminder-date', true);
 
    //We update the _cred_post_expiration_time Field in the Database with this new value
     update_post_meta($post_id, '_cred_post_expiration_time', $new_date);
	
  		
		$my_post = array(
			'ID'           => $post_id,
                      'post_status' => 'publish'
             
        );
 
        // Update the post into the database
        wp_update_post( $my_post );
    }
}

This is working fine. But is there a way I can also set the field “After expiration change the status of the post to:” to Draft? Basically I want to have the email notification triggered at the date the user entered. So if I can set the post status to change at that date then I can set the form to fire when “When post status changes to: Draft”. That’s my ultimate goal.

Thank you,

Tim

#598381

Nigel
Supporter

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

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

Screen Shot 2017-12-13 at 10.11.26.png

Hi Tim

Am I missing something here?

The status of the post after expiration is just one of the settings on the form, see screenshot.

You don't need to set it in code, so in the code sample you provided above, everything (apart from closing brackets) after the update_post_meta line is not required.

On form submission your code sets the post expiry time according to a field in the form itself.
According to your settings the post status is changed to draft on post expiry.
Your notification email is triggered by the change in status.

Did I misunderstand?

#598908

Sorry, you are right, I didn't think the setting in the Cred form to change it to Draft was working, but it is. The issue then is that the notification email isn't being sent. On the Cred form I have it set to send the email when " When post status changes to: " Draft. And I see it does change to Draft upon expiration. So something isn't working as it should...

Tim

#598914

Nigel
Supporter

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

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

Hi Tim

Let me test this locally to confirm that it should work.

I've set it up and published a test post, I'll need to wait an hour or so for the CRON job to run that expires the post, and I'll report back to you afterwards.

#598960

Nigel
Supporter

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

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

Hi Tim

So in my test it failed. The notification was not sent.

The notification works because if re-publish the post in the backend and then revert its status to draft the notification is then triggered.

I haven't seen anywhere that this should work, but then I haven't seen anything to suggest it shouldn't, so I'm escalating this thread so that it can be investigated further.

I'll keep you posted with any news.

#598967

Hi Nigel,

Thanks for the assistance. But I'm not sure what you mean by "The notification works because if re-publish the post in the backend and then revert its status to draft the notification is then triggered." In the CRED form there's a setting to have the email notification sent upon post status change. In my setup when the expiration date happens the post status changes from Published to Draft so the email should be triggered. No?

Tim

#598976

Nigel
Supporter

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

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

Yes, that's right, but it's not working, which is why I have escalated this thread.

I was just explaining that if you alter the status of a post manually and set it to draft you should find that the notification is sent. But sending it when the post is automatically expired fails.

Sorry if I confused you.

#599343

This is expected.

If you add a notification by post status change to a form with expiration, it will be fired as the post status changes unless it is the expiration that changes it.

Please add a notification that triggers either when the post is expired - or do not change the post status with the expiration if you want to send an email when the status changes.

This will not change in future, but we might add hints and adequate descriptions so the confusion will be sort out at it's roots.

#599350

Hmmm, I'm confused. In the Cred form settings where it says "When to send this notification:" there is an option to have it send when the post status changes to Draft. So you're saying it actually doesn't work that way? But then.. what does that mean?

Also you said, "Please add a notification that triggers either when the post is expired - or do not change the post status with the expiration if you want to send an email when the status changes." I thought my method was the way to trigger the notification when the posit is expires. Is there another way? And if I "do not change the post status with the expiration if you want to send an email when the status changes" then what other method do I have to automatically send the notification email?

I must be missing something here.

Tim

#600407

Nigel
Supporter

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

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

Screen Shot 2017-12-20 at 07.36.49.png

Hi Tim

If the objective is simply to send an email notification when the post expires, then you can send the notification 0 minutes before the post expires (see screenshot).

Your case is an edge case because you are modifying the post expiration time after submission based upon the choice the user makes in the form, but I would still expect it to work.

Can you try that option for triggering your notifications, and if the notification isn't sent let me know and I'll see what's required for it to work when changing the post expiration date.

#600461

Thanks, I will try this and let you know tomorrow if it works.

Tim

#600485

Nigel
Supporter

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

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

OK, keep me posted. I expect it will.

#601029

After some tests the email notifications are being sent on the day specified in the form, but now they are empty of any fields I had added in the email body. It seems it is now not associating the fields in the email with any post so the emails are empty.

This is what I have in the Body of the email:

<h2>%%POST_PARENT_TITLE%%</h2>

[wpv-post-link id="$directory-listing"] 
[types field="drink-details" id="$directory-listing" separator=', '][/types]
[types field="food-details" id="$directory-listing" separator=', '][/types] 

Time: [types field='happy-hour-start' id="$directory-listing"][/types]:00 - [types field='happy-hour-end' id="$directory-listing"][/types]:00 

[types field='location-address' id="$directory-listing"][/types] 

Sincerely, Happy Saigon!

Also I'm not specifying the hour/minute time with the form and I see that it is automatically set to 7am in the "Setting for post expiration date". However that time isn't indicative of when the email is actually sent. If I adjust that to, for example, 5 minutes from now, the post does not expire in 5 minutes and the email is not sent. (my timezone is UTC+7, and I have that set in the Settings)

Tim

#601073

Nigel
Supporter

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

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

Hi Tim

When using Types or Views shortcodes in the notification emails you need to maintain the connection with the post in question by adding the attribute id="%%POST_ID%%".

That means that you cannot also refer to parent posts as you currently do with id="$directory-listing", it won't work.

Our CRED notification system is under review, so it might become possible, but for now it is not.

Regarding the timing of when posts expire, there is a CRON job that runs periodically and expires any posts whose expiration time occurred in the interval since the last time it ran.

So posts won't necessarily expire at an exact time.

You can change the settings for how often the CRON job runs at Toolset > Settings > Forms, with hourly the minimum interval.

Note that they way WordPress CRON jobs operate, there needs to be some activity on a site for the batch job to be triggered. If no-one visits your site for a day, front-end or back-end, the CRON job won't run that day.

#601277

Thanks for the tip about adjusting the Cron job interval.

I replaced id="$directory-listing" with id="%%POST_ID%%" but the emails are still blank.

<h2>[wpv-post-link id="%%POST_ID%%"]</h2>

<p> [types field="drink-details" id="%%POST_ID%%" separator=', '][/types] </p>
<p> [types field="food-details" id="%%POST_ID%%" separator=', '][/types] </p>

Time: [types field='happy-hour-start' id="%%POST_ID%%"][/types]:00 - [types field='happy-hour-end' id="%%POST_ID%%"][/types]:00

<p> [types field='location-address' id="%%POST_ID%%"][/types] </p>

<p>Sincerely, Happy Saigon!</p>

Tim