2

I'm working on a template for a specific category archive of the custom post type event.

I need to make a query that shows future and ongoing events, ordering by a date in custom field.

$termName = get_queried_object()->name;
$termSlug = get_queried_object()->slug;
$event1 = current_time('Y-m-d');

$args = array(
    'post_type' => 'event',
    'event-categories' => $termSlug,
    'post_status' => 'publish',
    'posts_per_page' => 10,
    'order' => 'ASC',
    'meta_query' => array(
        'relation'    => 'OR',
        array(
            'relation'    => 'AND',

        array(

            'key' => '_event_start_date',
            'value' => $event1,
            'compare' => '>=',
            'type' => 'DATE',

        ),


        array(

            'key' => '_event_end_date',
            'value' => $event1,
            'compare' => '>=',
            'type' => 'DATE',

        )
        ),
        array(
            'key' =>  $event1,
            'value' => array('_event_start_date','_event_start_date'),
            'type' => 'DATE',
            'compare' => 'BETWEEN'
        )

    ),

     'orderby' => '_event_start_date',
 );
$events = new WP_Query($args);
echo $events->post_count   ;

The result shows only the future events, but not ongoing events.

What's wrong?

1

Simpler CODE:

Logically, when you check >= for both _event_start_date and _event_end_date, that automatically covers the BETWEEN check.

So the following CODE should work:

$termName = get_queried_object()->name;
$termSlug = get_queried_object()->slug;
$event1 = current_time( 'Y-m-d' );

$args = array(
    'post_type'        => 'event',
    'event-categories' => $termSlug,
    'post_status'      => 'publish',
    'posts_per_page'   => 10,
    'order'            => 'ASC',
    'meta_query' => array(
        'relation'    => 'OR',
        array(
            'key'     => '_event_start_date',
            'value'   => $event1,
            'compare' => '>=',
            'type'    => 'DATE',
        ),
        array(
            'key'     => '_event_end_date',
            'value'   => $event1,
            'compare' => '>=',
            'type'    => 'DATE',
        )
    ),
    'orderby' => '_event_start_date',
);
$events = new WP_Query( $args );
echo $events->post_count;

Date check:

Also, make sure the date value $event1 is properly set as the one stored in Database (in post meta table). Normally, WordPress internally considers all date to be in the GMT timezone. However, that may vary depending on how the _event_start_date and _event_end_date is saved in your case. It can be different depending on the stored value:

// MySQL DATETIME field format
$event1 = current_time( 'mysql' );

// MySQL DATETIME field format in GMT
$event1 = current_time( 'mysql', 1 );

// Unix timestamp in local time zone
$event1 = current_time( 'timestamp' );

// Unix timestamp in GMT
$event1 = current_time( 'timestamp', 1 );

So, better check how that data is saved, or test with these variances. If date fields include time as well, then you may also work with DATETIME type.

Also, a note from codex:

The 'type' DATE works with the 'compare' value BETWEEN only if the date is stored at the format YYYY-MM-DD and tested with this format.

tax Query :

The old tax query is deprecated. So instead of using:

'event-categories' => $termSlug,

use:

'tax_query' => array(
    array(
        'taxonomy' => 'event-categories',
        'field'    => 'slug',
        'terms'    => $termSlug,
    ),
),

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.