Skip Navigation

[Resolved] Set automatic post expiry to the day after custom date field

This support ticket is created 5 years, 6 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
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 4 replies, has 2 voices.

Last updated by leilaG 5 years, 6 months ago.

Assisted by: Beda.

Author
Posts
#1296523

How would we set the post expiry to the day after a custom date field?

We have added this code:

//Set the post expiry date to the day after the "Departure date" field
add_action('cred_save_data','set_expiration_func',10,2);
function set_expiration_func($post_id,$form_data) {
if ($form_data['id']== 10466) {
$event_start = get_post_meta($post_id, 'departure-date', true);
if($event_start){
update_post_meta($post_id, '_cred_post_expiration_time', $event_start);
}
}
}

#1296693

If you require custom code ready to go, or further help on custom coding, I would suggest contacting a https://toolset.com/contractors/ because Toolset Support can assist custom coding only limitedly.

1. As far I see, the code does not add a day to the date of the custom field, currently.

==> Did you try to print_r or var_dump() the variables you populate in your code? This is the first step in creating such code, as it helps you to understand what the data looks like and how to handle it.

2. Another thing to note is, Toolset Fields are saved with a wpcf-prefix.
Your current get_post_meta does not make usage of that, it should be:
$event_start = get_post_meta($post_id, 'wpcf-departure-date', true);

==> As mentioned, this will be easily visible as soon you var_dump() or print_r() the $variables you get in the code, it will show if you have or not expected data.

3. To add a day to a date stored in a Custom Field, if that is as Toolset Date field you will originally receive a Timestamp from the get_post_meta($post_id, 'departure-date', true);
A timestamp is a number, counting seconds from a certain date: hidden link

Now, to add one day to that precise date, you can for example simply add the value in seconds for 24 hours, but smarter it is to use PHP function that does the trick:
strtotime()
hidden link
Something like strtotime('+1 day', $timestamp); should do.

4. This allows you to add a day to your original timestamp.
So now you can update this new date to the Expiration date of the post.
That can be done directly in timestamp format as the expiration date is also stored as a timestamp.

Right now you miss the addition of 24 hours or a day to your code, however, and the prefix of the field is not yet correct.

Please let me know if you can achieve the code with the additional details.

#1297595
visitor-target-date.jpg

Thanks for the info, we are not that familiar with code, so may have to use the contractors for this.

Until then if we wanted to hide all posts from the view that have departure dates +1day after the current date , by adding a filter, how would we do that? if we are already using the date filters to get a target date. (image attached of date filters we are using already - https://toolset.com/forums/topic/user-date-filter-search-between-dates-2/)

#1298351

I understand.

The filter you mention is rather another issue and should be pursued in another ticket - but I'll try to reply right here.

1. You'd create a View listing those posts
2. Set a Query filter against that Date field and compare to "a number greater than FUTURE_DAY(1)"
This should set the filter to show only posts where the (timestamp) date Custom field is greater than tomorrow (which is +1 day after current date, so to speak)
See also https://toolset.com/documentation/user-guides/date-filters/

I hope that helps to achieve the goal, please if not could you open a new dedicated ticket for it?

#1298609

Sure I will open a new ticket. Thank you