Support

Account

Home Forums Backend Issues (wp-admin) Date Time Picker and Post Loop

Solved

Date Time Picker and Post Loop

  • I’m hoping someone might be able to assist me. I’m utilizing ACF Pro for creating a Date Time Picker field for showing events. I am trying to only showing posts that have a future date time picker value. Here’s what I got and it worked using the old Date and Time Picker add-on for the non-Pro version but now I’m trying to get it to work with the pro-version after included it natively. Here’s the query I was using and am baffled as to why it isn’t working. The idea, is that after the event_start_date has passed, no longer show the post. event_start_date is the ACF Date Time Picker field.

    $currentdate = getdate()[0];
    query_posts( array( ‘posts_per_page’ => 3,
    ‘ignore_sticky_posts’ => 1,
    ‘post_type’ => ‘events’,
    ‘meta_query’ => array(
    array(
    ‘key’ => ‘event_start_date’,
    ‘compare’ => ‘>=’,
    ‘value’ => $currentdate,
    ‘type’ => ‘numeric’,
    )
    ),
    ‘meta_key’ => ‘event_start_date’,
    ‘orderby’ => ‘meta_value_num’,
    ‘order’ => ‘ASC’,
    ‘paged’ => ( get_query_var(‘paged’) ? get_query_var(‘paged’) : 1 )
    ) );

  • Hi @poorpaddy

    ACF PRO saves the date time picker field in a MySQL’s date time format (yyyy-mm-dd hh:mm:ss). So, you need to change how you get the current date time and the ‘type’ query like this:

    $currentdate = date('Y-m-d H:i:s');
    ...
    'type' => 'DATETIME',

    This page should give you more idea about it: https://www.advancedcustomfields.com/resources/date-time-picker/.

    I hope this helps 🙂

  • That worked at showing the correct next date in the future but it is no longer showing later dates after that. Has me baffled.

  • Hi @poorpaddy

    The old date-time add-on uses a timestamp to save the data in the database. If you don’t have a lot of posts, I suggest you re-save the old posts instead. This will make sure that it’s compatible with WordPress and ACF in the future.

    If you don’t want to change the old data, then you can always convert the new date-time value to a timestamp instead. I believe you can do it by using this code:

    function my_acf_save_post( $post_id ) {
        
        // Set the date time field name
        // change with the field key if it doesn't work
        $date_time_field_name = 'event_start_date';
        
        // get the saved date time value
        $datetime = get_field($date_time_field_name, $post_id, false);
        
        // if it exists
        if( $datetime ){
            
            // convert it to timestamp
            $datetime_timestamp = strtotime($datetime);
            
            // Update the value in the database
            update_field( $date_time_field_name, $datetime_timestamp, $post_id );
            
        }
        
    }
    
    // run after ACF saves the $_POST['acf'] data
    add_action('acf/save_post', 'my_acf_save_post', 20);

    Don’t forget to re-save the newly created post to convert the date-time value.

    I hope this helps 🙂

  • That was the fix!! Thank you so much. Yes, I believe the old time date picker used the timestamp and I was both comparing against that stamp and ordering by it.

    I tried doing it the new method but it just wouldn’t work for me.

Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘Date Time Picker and Post Loop’ is closed to new replies.