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

Last updated by avansisI-2 3 years, 4 months ago.

Assisted by: Christian Cox.

Author
Posts
#1852477

I'm creating a custom type that has a custom field date, and I want the expired text to appear if 2 days pass from the creation date.

So I need to know how to automatically fill in this field when the custom type is created, and set the conditional if 2 days have passed since the creation.

Could you help me?
thanks

#1852917

So I need to know how to automatically fill in this field when the custom type is created
If the post type is created in wp-admin, you can use the save_post hook to automatically set a Types date field value:

add_action( 'save_post', 'tssupp_auto_set_date_field', 100, 3 );
function tssupp_auto_set_date_field( $post_id, $post, $update ) {
  $post_type = 'post-type-slug';
  $field_slug = 'your-date-field-slug';
  $field_value = get_post_meta($post_id, 'wpcf-' . $field_slug, true);
  if ( $post->post_status == 'publish' && $post->post_type == $post_type && !$field_value ) {
    update_post_meta( $post_id, 'wpcf-' . $field_slug, date('U') );
  }
}

You may need to refresh the screen in wp-admin to see the value in the date field if you're using the Block Editor.

If the post is created in Forms, you can use the cred_save_data API instead to do something similar when the post is created:

add_action( 'cred_save_data', 'tssupp_auto_set_date_field', 10, 2 );
function tssupp_auto_set_date_field( $post_id, $form_data ) {
  $form_id = 12345; // your form ID
  $field_slug = 'your-date-field-slug';
  $field_value = get_post_meta($post_id, 'wpcf-' . $field_slug, true);
  if ( $form_data['id'] == $form_id && !$field_value ) {
    update_post_meta( $post_id, 'wpcf-' . $field_slug, date('U') );
  }
}

To test if the date field is greater than 2 days ago, you must write a custom function or shortcode that calculates the difference between the current date/time and the custom field date/time value. Types custom date fields store dates in Unix timestamp format, so you would need to get the current date/time as a Unix timestamp in PHP:

$current_timestamp = date('U');

To get the Unix timestamp value of a Types date field from the database, you can use get_post_meta:

$post_id = 12345;
$field_slug = 'your-date-field-slug';
$timestamp = get_post_meta($post_id, 'wpcf-' . $field_slug, true);

Then you can subtract the $timestamp from $current_timestamp and compare the result against the number of seconds in 2 days - (2 * 60 * 60 * 24, i.e. 172800); If the subtracted value is larger than 172800, you know the post's creation date field is greater than 48 hours older than the current timestamp. Your shortcode or custom function should return a value you can compare against in the conditional.

#1853487

In reality the user does not have to know that the date is being created, it would be the same as long as it is later shown in the information of that bonus.

On the other hand, the expiry date 2 days later means that the bonus has expired.

Sorry if I didn't make myself clear

#1853591

Okay, so the issue is resolved now? Or do you have a question about the examples I provided? I'm not clear about what you need from me to implement a conditional based on a Types date custom field value.

#1853745

Right now I don't know where I have to put that code so that when a user creates the voucher the date of the moment is saved automatically.

I add it in the functions.php?

#1853827

Yes, you can add the customized code in your custom theme's functions.php file, or you can add it in a custom code snippet in Toolset > Settings > Custom Code tab. Either location is okay.

#1853987

Thanks Christian,

I think understand it but i have any question.

this is my code

// fecha automatica bonos
// 
add_action( 'save_post', 'tssupp_auto_set_date_field', 100, 3 );
function tssupp_auto_set_date_field( $post_id, $post, $update ) {
  $post_type = 'bonos';
  $field_slug = 'fecha-de-creacion-del-bono';
  $field_value = get_post_meta($post_id, 'wpcf-fecha-de-creacion-del-bono' . $field_slug, true);
  if ( $post->post_status == 'publish' && $post->post_type == $post_type && !$field_value ) {
    update_post_meta( $post_id, 'wpcf-fecha-de-creacion-del-bono' . $field_slug, date('U') );
  }
}

I think this is ok

But this is wrong

 
add_action( 'cred_save_data', 'tssupp_auto_set_date_field', 10, 2 );
function tssupp_auto_set_date_field( $post_id, $form_data ) {
  $form_id = 5908; // your form ID
  $field_slug = 'fecha-de-creacion-del-bono';
  $field_value = get_post_meta($post_id, 'wpcf-fecha-de-creacion-del-bono' . $field_slug, true);
  if ( $form_data['id'] == $form_id && !$field_value ) {
    update_post_meta( $post_id, 'wpcf-fecha-de-creacion-del-bono' . $field_slug, date('U') );
  }
}

After we have problems with this also

$current_timestamp = date('U');

What is post_id?

$post_id = 12345;
$field_slug = 'fecha-de-caducidad';
$timestamp = get_post_meta($post_id, 'wpcf-' . $field_slug, true);

Thanks

#1855299
Screen Shot 2020-11-24 at 9.22.45 AM.png

Do you want to set the date when posts are created in wp-admin, when posts are created in Forms, or both?
- If you want to set the date automatically when posts are created both in wp-admin and in Forms, use the save-post hook and delete the cred-save-data hook. You do not need both hooks.
- If you want to set the date automatically only when posts are created in Forms, keep the cred-save-data hook and delete the save-post hook.

After we have problems with this also

$current_timestamp = date('U');

What exactly is the problem? You may need to refer to PHP's documentation related to generating a current Unix timestamp in your server's PHP version, for example, time(): https://www.php.net/manual/en/function.time.php
Or date(): https://www.php.net/manual/en/function.date.php

What is post_id?

$post_id = 12345;

This code is just an example using a hard-coded value. You must customize it to work for your needs. Your code will probably use a variable instead of a hard-coded value. You must determine the correct post ID, depending on where you place this code. For examples, see the screenshot. In each case, $post_id is available as an argument in the callback function. Otherwise, you must use some WordPress API or Toolset API to fetch a specific post and determine the correct post ID.

#1855319
Captura de pantalla 2020-11-24 a las 15.37.56.png

My post type is called bonos (add an image)

is it correct?

// fecha automatica bonos
// 
add_action( 'save_post', 'tssupp_auto_set_date_field', 100, 3 );
function tssupp_auto_set_date_field( $bonos, $post, $update ) {
  $post_type = 'bonos';
  $field_slug = 'fecha-de-creacion-del-bono';
  $field_value = get_post_meta($bonos, 'wpcf-fecha-de-creacion-del-bono' . $field_slug, true);
  if ( $post->post_status == 'publish' && $post->post_type == $post_type && !$field_value ) {
    update_post_meta( $bonos, 'wpcf-fecha-de-creacion-del-bono' . $field_slug, date('U') );
  }
}

For time function maybe this can work?

$fecha_actual = date("d-m-Y");
//sumo 2 días
echo date("d-m-Y",strtotime($fecha_actual."+ 2 days")); 

But, how i can relation it?

#1855541

The code can be updated like this:

add_action( 'save_post', 'tssupp_auto_set_date_field', 100, 3 );
function tssupp_auto_set_date_field( $bonos, $post, $update ) {
  $post_type = 'bonos';
  $field_slug = 'fecha-de-creacion-del-bono';
  $field_value = get_post_meta($bonos, 'wpcf-' . $field_slug, true);
  if ( $post->post_status == 'publish' && $post->post_type == $post_type && !$field_value ) {
    update_post_meta( $bonos, 'wpcf-' . $field_slug, date('U') );
  }
}

For time function maybe this can work?
It depends on what time you want to store in the field. If you want to store the timestamp when the post was created, you should not add 2 days when you insert the postmeta value. You should store the current timestamp, date('U'). If you want to store the timestamp when the post expires, then you should store the timestamp of two days in the future. The format for timestamp is 'U', not 'd-m-Y'. Toolset date fields will not work when you store a date in the format 'd-m-Y', only the format 'U' for epoch timestamp.

Based on the field name, fecha-de-creacion-del-bono, it sounds like the field should store the time the post was created, not the expiration date. Then in your custom shortcode or custom function you can subtract the created timestamp field value from the current timestamp (date('U')) and compare the result against the number of seconds in 2 days - (2 * 60 * 60 * 24, i.e. 172800). If the subtracted value is larger than 172800, you know the post's creation date field is greater than 48 hours older than the current timestamp. Your shortcode or custom function should return a value you can compare against in the conditional, like 1 if the post is expired or 0 if the post is not expired.

#1855607

My approach was the following.

- The date / time is created with the (U) function when the coupon is created
- The expiration date is also automatically created, if the expiration date is less than the current date, the coupon has an expired status.

But what you indicate is still easier, would the expiration-date field then be left over?

add_action( 'save_post', 'tssupp_auto_set_date_field', 100, 3 );
function tssupp_auto_set_date_field( $bonos, $post, $update ) {
  $post_type = 'bonos';
  $field_slug = 'fecha-de caducidad;
  $field_value = get_post_meta($bonos, 'wpcf-' . $field_slug, true);
  if ( $post->post_status == 'publish' && $post->post_type == $post_type && !$field_value ) {
    update_post_meta( $bonos, 'wpcf-' . $field_slug, date('U' + 172800) );
  }
}
#1855677

I'm not sure I understand. If you store the creation date in a custom field, your custom function or custom shortcode can compare that timestamp against the current timestamp without the need for storing the expiration date in a custom field:

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 = 'your-date-created-field-slug'; // edit your created field slug here
  // do not edit below this line
  $created_field_value = get_post_meta($bonos, 'wpcf-' . $created_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') );
  }
}

However, if you want to store both the created date and the expiration date in custom fields, you may set up two custom date fields to store this information in Toolset > Custom Fields > Post Fields. Then you can use this code to set both field values automatically:

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 = 'your-date-created-field-slug'; // edit your created field slug here
  $expiration_field_slug = 'your-date-expiration-field-slug'; // 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 code will store both the created value and the expiration value in custom fields. Then your custom function or custom shortcode can be used to test the value of the expiration timestamp against the current timestamp. You could store one or both values in custom fields, it is up to you. It depends on whether or not you want to store the expiration date in a separate custom field for some reason - for example, if you want to easily filter a View of Bonos posts based on the expiration date. Then it makes sense to store the expiration date as well as the creation date.

#1855685

I think is better second option, to create a conditional, but

This code, is not working

//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) );
  }
}
#1855713
Screen Shot 2020-11-24 at 1.34.35 PM.png

In wp-admin I can see that these two date fields are required. If the fields are required, you cannot publish the post from wp-admin without setting some date value in both fields. This stops the automatic field value code from working, because the code does not overwrite an existing value. So I suggest you do not make these fields required if you plan to create these posts in wp-admin, or you must modify the code to work for your custom requirements.

If you plan to only create these posts in Forms, I will need to see the Form on the front-end of your site. Can you provide a URL so I can test?

#1855723

I want to create it from

/form-bonos/

I wan to create only with button generate. When you click generate coupon is create with a random number (i have another tickets with this random number)

maybe you can help me

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.