Skip Navigation

[Resolved] Update custom field date via function

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.

Our next available supporter will start replying to tickets in about 6.67 hours from now. 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/Karachi (GMT+05:00)

Author
Posts
#1244404

Hi

I’m currently using the plugin CalendarizeIT with Toolset. I’d like to display the post entries as future events and hide the old entries (older than today – hidden link). The custom field used to generate the date is “fc_start” which comes from CalendarizeIT ( hidden link ).

I did use this filter hidden link but it doesn’t work. I assume then that “fc_start” doesn’t store timestamp value.

Since I understood there’s no way to make “fc_start” act like Toolset’s default time field ( hidden link ) is there a way to automatically fill Toolset’s default time field with the contents of “fc_start” so that I can then prevent the older posts entries from appearing?

I also understood that one could build a custom function following this tutorial https://toolset.com/documentation/customizing-sites-using-php/updating-types-fields-using-php/. That function will get the assigned value from the field “fc_start”, convert it into UNIX timestamp format and then add it to Toolset new date type custom field.

I tried to use this function but it doesn't work

function udpate_event_datefield( $post_id, $post ){
if ( 'event-date-field' == $post->post_type ) {
$deadline = get_post_meta( $post_id, 'wpcf-deadline', true );
$now = time();
if ( !empty($deadline) && $now > $deadline ) {
update_post_meta( $post_id, 'fc_start', 1 );
}
}
}
add_action( 'save_post', 'udpate_event_datefield', 30, 2 );

Thank You

#1244422

Hi Marian,

Thanks for asking! I'd be happy to help.

Can you please share temporary admin user's login details, so that I can see how these fields are set up?

Note: Your next reply will be private and though no changes will be made on your website, please make a complete backup copy, before sharing the access details.

regards,
Waqar

#1244587

Hi Marian,

Thank you for sharing the access details.

I'll recommend the following steps:

1. I've noticed that you've added the Toolset's date type custom field with the slug "date-start".

To automatically populate its value, based on the value selected in the "fc_start" field, please update your custom function to:


function auto_update_date_field( $post_id ) {
   
    if ( get_post_type( $post_id ) == 'events' ) {
        
        $fc_start = get_post_meta( $post_id, 'fc_start', true );

        if(!empty($fc_start)) {

        	$date_obj=date_create($fc_start);

        	update_post_meta( $post_id, 'wpcf-date-start', date_format($date_obj,"U") );
        }
        
    }
        
}
add_action( 'save_post', 'auto_update_date_field', 99 );

As a result, when a new or existing event will be updated from the admin area, this new date field's value will be automatically updated too.

2. In your view's "Query Filter" section, add a filter for this new date type custom field as shown in this example screenshot:
hidden link

I hope this helps and let me know how it goes.

Note: For more personalized assistance around custom code, you can also consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/

regards,
Waqar

#1244675

Awesome Help. My issue is resolved now. Thank you!