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.
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.
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...
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.
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.
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?
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.
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.
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?
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.
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.
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)
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.