Navigation überspringen

[Gelöst] Set post expiration only after the post changes to published

This support ticket is created vor 4 Jahren, 9 Monaten. 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
9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 - - 9:00 – 13:00
14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 - - 14:00 – 18:00

Supporter timezone: Africa/Casablanca (GMT+01:00)

Dieses Thema enthält 6 Antworten, hat 2 Stimmen.

Zuletzt aktualisiert von Jamal vor 4 Jahren, 9 Monaten.

Assistiert von: Jamal.

Author
Artikel
#1717635

My use case is this:
1) The user submits a form that creates a post and sets its status to Pending Review. It also sets expiration to, say, 1 week later
2) The admin reviews the post and changes its status to published (uncheck the pending review status). The post expiration's timer starts right now

My observations:
When setting expiration options on the form, I was lead to believe that my use case above would be addressed already. The form instructions read as following: "Post will expire in ___ from the *publish* date of the post."

According to my observations instead, the expiration timer is set to when the post is created, regardless of its status, which to be fair is in line with the documentation (https://toolset.com/course-lesson/extending-form-functionality-with-hooks/).

Questions being:
1) Is the observed behavior above correct?
2) Perhaps to avoid confusion, shouldn't the form guidelines changed to: "Post will expire in ___ from the *creation* date of the post."
3) Any suggestion I can implement my use case? I guess with some hook and custom coding.

Thank you

#1718031

Hello and thank you for contacting the Toolset support.

1) Is the observed behavior above correct?
Yes, the expiration should be tied to the form date. If the form creates a post, the expiration should tied to this date. If the form is for editing, the expiration should be tied to this edit date.

2) Perhaps to avoid confusion, shouldn't the form guidelines changed to: "Post will expire in ___ from the *creation* date of the post."
Let me escalate this to our 2nd Tier to get another opinion or confirmation.

3) Any suggestion I can implement my use case? I guess with some hook and custom coding.
I would suggest, creating an edit form to let admin change the status from "Pending Review" to "Publish" and configuring the expiration on this form.
With custom code, you can do that too. The expiration date is saved as timestamp in a post meta called _cred_post_expiration_time . You can create an action on the transition_post_status hook and update the expiration date accordingly. Check this sample code, please test it to confirm if it is working for you:

// Add the hook action
add_action('transition_post_status', 'send_new_post', 10, 3);

// Listen for publishing of a new post
function send_new_post($new_status, $old_status, $post) {
  if('publish' === $new_status && 'publish' !== $old_status && $post->post_type === 'your-cpt-slug') {
    // Expire post in  7 days
    $expiry_timestamp = time() + ( 7 * 24 * 60 * 60 );
    update_post_meta($post->ID, '_cred_post_expiration_time', $expiry_timestamp );
  }
}
#1718149

Thank you so much, Jamal.

I will go for one of the two proposed solutions by you. I would prefer to use the hook, so that the admins can manage everything from the admin console rather than going through a form on the website.

I understand this might be out of your support scope, but I'm facing a problem implementing the hook, and wondering if you have any clue. This is what happens:

1) If the status is updated from the Quick Edit (in admin), it works, the expiration date is updated, though strangely enough, nothing's logged in my log file. However;
2) If the status is updated from the post edit page (in admin), the expiration date is not updated, though strangely enough, I have entries in my log file.

Something definitely odd is going on

#1718169

I would suggest starting with a simple hook that will only log a string to your log file, and test it from both locations(quick edit, and post edit):

// Add the hook action
add_action('transition_post_status', 'send_new_post', 10, 3);
 
// Listen for publishing of a new post
function send_new_post($new_status, $old_status, $post) {
  if('publish' === $new_status && 'publish' !== $old_status && $post->post_type === 'your-cpt-slug') {
    error_log('Hello');
  }
}

This way, you will be sure that the hook is executed in both cases. Then you can continue with debugging your code.

If instead, the hook is only executed in the quick edit screen and not on the post edit screen in the admin area, you may need to use a different hook(save_post) an additional custom field that will hold the date where you have updated the expiration date. This way, you can be sure that you will not set a new expiration date every time the post is updated.

#1718171

Thank you, Jamal.

I have already tried basic debugging and the outcome was my previous observations. I think I won't go for the post_save hook, as it's a bit too complex implementation. I think I will go for the cred form solution.

I'm marking this ticket as resolved as my initial questions were answered. Thank you very much

#1718173

My issue is resolved now. Thank you!

#1720571

Thank you for your feedback. I just wanted to let you know, that we have escalated the message error to our developers.
Thank you for reporting this.