Hi,
I am trying to: Automatically set the post expiry date to the "Event Start" date field, this is to prevent users from seeing old events which are in the directory.
I expected to see: An option to do this easily and be able to set the expiry date to a field created in Toolset
Instead, I got: Just an option of how many days,weeks,months and years until expiry. I would like to auto-set the date based on the date of the event happening.
In essence I would like: User adds event to the directory -> event show on directory until the event date -> post is removed from the directory on the date the event starts.
Please can you help? 😀
Thanks,
John
Dear John,
There isn't such a built-in feature within CRED form, CRED is using a hidden field "_cred_post_expiration_time" to store value of "Post Expiry date" in time-stamp format,
So you can try these:
1) When user submit the CRED form in front-end, use CRED action hook "cred_save_data" to trigger a PHP function
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
2) In the custom PHP function, update custom field "_cred_post_expiration_time" value as the custom date field value
https://codex.wordpress.org/Function_Reference/update_post_meta
Is it possible for someone to help me do this?
I do not have PHP experience so would not know how to embed this. It should be a very simple function which you would think is included with the package. Given there is already a post-expiry function no?
Thanks,
John
For example, you can try to add below codes into your theme/functions.php:
//Set the post expiry date to the "Date of event" field
add_action('cred_save_data','set_expiration_func',10,2);
function set_expiration_func($post_id,$form_data) {
if ($form_data['id']== 123) {
$event_start = get_post_meta($post_id, 'wpcf-event-start', true);
if($event_start){
update_post_meta($post_id, '_cred_post_expiration_time', $event_start);
}
}
}
Please replace 123 with your CRED form ID, replace "wpcf-event-start" with the field slug of "Event Start" date field
Hi Support Team,
I have a quick question for your above function, As I can see, the above function allows to add the "wpcf-event-start" dateto be considered as expiry date and As I understand, this function will automatically set "wpcf-event-start" date value as default expiry date. But as I see in the function, It needs form ID(TOOLSET POST FORM) but for my case, I use third party form to add posts from frontend, I have an idea to create custom field and in the third party form, I can automatically save current date and then when form is submitted then and the custom fieldis filled with a date value then this custom function can trigger and update "cred_post_expiration_time" based on the value I got in the custom field?
Please let me know
//Set the post expiry date to the "Date of event" field
add_action('cred_save_data','set_expiration_func',10,2);
function set_expiration_func($post_id,$form_data) {
if ($form_data['id']== 123) {
$event_start = get_post_meta($post_id, 'wpcf-event-start', true);
if($event_start){
update_post_meta($post_id, '_cred_post_expiration_time', $event_start);
}
}
}