Enrico
Enrico

Reputation: 830

Combining meta_query and tax_query to fetch all future events (custom post)

I am using this WP query to fetch just the future events but the query returns all the events, even the past ones. What am I doing wrong? The date format is the same (j F Y) of the one coming from the ACF custom field and the tax_query is working because only the events belonging to the specified location are fetched (location_categories).

$events = array(
            'post_type' => 'event',
            'post_status'=>'publish',
            'meta_key' => 'event_start_date',
            'orderby' => 'meta_value',
            'order' => 'DESC',
            'posts_per_page'=>'6',
            'paged' => $paged,
            'tax_query' => array(
                array(
                    'taxonomy' => 'location_categories',
                    'field' => 'term_id',
                    'terms' => get_field('choose_event_location',$page_id)
                )
            ),
            'meta_query' => array(
                array(
                    'key' => 'event_start_date', 
                    'value' => date("j F Y"), 
                    'compare' => '>=', 
                    'type' => 'DATE'
                )
            )
        );

$loop = new WP_Query($events);

Upvotes: 0

Views: 211

Answers (1)

Bhautik
Bhautik

Reputation: 11282

ACF save date as 'Ymd'. try date("Ymd")

This is how ACF saves date.

enter image description here

Check the below code.

$events = array(
    'post_type'      => 'event',
    'post_status'    =>'publish',
    'meta_key'       => 'event_start_date',
    'orderby'        => 'meta_value',
    'order'          => 'DESC',
    'posts_per_page' =>'6',
    'paged'          => $paged,
    'tax_query' => array(
        array(
            'taxonomy' => 'location_categories',
            'field'    => 'term_id',
            'terms'    => get_field('choose_event_location',$page_id)
        )
    ),
    'meta_query' => array(
        array(
            'key'     => 'event_start_date', 
            'value'   => date("Ymd"), 
            'compare' => '>=', 
            'type'    => 'DATE'
        )
    )
);

$loop = new WP_Query($events);

Upvotes: 1

Related Questions