Skip Navigation

[Resolved] Set the post expiry date to the "Date of event" field?

This support ticket is created 6 years, 9 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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: Asia/Hong_Kong (GMT+08:00)

Author
Posts
#553740

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

#553790

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

#554320

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

#554526

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

#1640707

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);
        }
    }
}
This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.