Skip Navigation

[Resolved] Status expired when 2 days have passed from the date of creation

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

Problem: After two days have passed from the time of post creation, I would like to display some text on my post that says the post has expired.

Solution: Use two separate custom date fields to store the post creation timestamp and the post expiration timestamp. Use a custom function or custom shortcode to compare the expiration timestamp with the current timestamp. The following code example shows how to set the two requested custom field values:

//FECHA AUTOMÁTICA BONOS
add_action( 'save_post', 'tssupp_auto_set_date_fields', 100, 3 );
function tssupp_auto_set_date_fields( $bonos, $post, $update ) {
  $post_type = 'bonos';
  $created_field_slug = 'fecha-de-creacion-del-bono'; // edit your created field slug here
  $expiration_field_slug = 'fecha-de-caducidad'; // edit your expiration field slug here
  // do not edit below this line
  $created_field_value = get_post_meta($bonos, 'wpcf-' . $created_field_slug, true);
  $expiration_field_value = get_post_meta($bonos, 'wpcf-' . $expiration_field_slug, true);
  if ( $post->post_status == 'publish' && $post->post_type == $post_type && !$created_field_value ) {
    update_post_meta( $bonos, 'wpcf-' . $created_field_slug, date('U') );
  }
  if ( $post->post_status == 'publish' && $post->post_type == $post_type && !$expiration_field_value ) {
    update_post_meta( $bonos, 'wpcf-' . $expiration_field_slug, (date('U') + 172800) );
  }
}
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.

Our next available supporter will start replying to tickets in about 8.73 hours from now. Thank you for your understanding.

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 18 replies, has 2 voices.

Last updated by avansisI-2 4 years ago.

Assisted by: Christian Cox.

Author
Posts
#1855733
Screen Shot 2020-11-24 at 2.13.34 PM.png

Okay the automatic custom field seems to work for me. I just created this post using the Form:
hidden link

The custom fields are both set. See the screenshot.

#1855883

Yes! this is great and work fine.

But if only have a button called generate. Could i can create an action to complete 'Referencia del bono' automatically

#1855897

Yes, of course. You could add custom code in the existing tssupp_auto_set_date_fields function to set the reference custom field value programmatically, or you could create another function and a separate save_post hook if you want to separate these functions. It looks like you have another ticket open for this issue: https://toolset.com/forums/topic/generate-a-random-number-and-letters-in-custom-field/
Another supporter has been assigned that ticket, and will follow up with you as soon as possible.

#1855937

My issue is resolved now. Thank you!