Support

Account

Home Forums General Issues Filtering posts by date (Date Picker)

Solving

Filtering posts by date (Date Picker)

  • Hi everyone,

    New to the forum I have a problem with my query. I have a post type ‘event’ with the field, event_date which is a Date Picker. I’m trying to filter between upcoming events and past events. I have tried to do my own research but I’m stuck now, and I keep getting ‘no posts’. Help is greatly appreciated. Thank you!

    My code is below:

    
    <?php
                    //find date time now
                    $today = date('Ymd');
                    //query
                    $posts = new WP_Query(array(
                        'posts_per_page' => 5,
                        'post_type' => 'event',
                        'order' => 'ASC',
                        'orderby' => 'meta_value',
                        'meta_key' => 'event_date',
                        'meta_type' => 'DATETIME',
                        'meta_query' => array(
                            array(
                                'key' => 'event_date',
                                'compare' => '<=',
                                'value' => $today,
                                'type' => 'DATE'
                            )
                        )  
                    ));
                    ?>
                    
                    <?php
                    $children = new WP_Query($posts);
                    if($children->have_posts())://display
                    else:echo "no posts";
    
  • Date fields in ACF are not stored in standard SQL date format, which is seems you are aware of looking at this line of your code $today = date('Ymd'); and this is the reason your query is getting no results. Change the type of the meta field to either ‘NUMERIC’ or let it default to ‘CHAR’.

  • It’s not working for me either!

    I want to show posts with event_date field greater or equal than today.
    Here’s my code, but the posts are not filtered. I tried without type or with NUMERIC or CHAR:

    
    $today = date('Ymd');
    $meta_query = array(
    	array(
    	'key' => 'event_date',
    	'compare' => '>=',
    	'value' => $today,
    	'type' => 'CHAR'
    	)
    );
    
    $args = array(
    	'post_type' => 'post',
    	'meta_key' => 'event_date',
    	'orderby'   => 'meta_value',
    	'order' => 'ASC',
    	'paged' => 1,
    	'meta-query' => $meta_query
    );
    
    $wp_query = new WP_Query($args);
    
Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.