0

I need to get posts pertaining to a given category, AND :

  • have the event_start_date (custom field) greater or equal than today's date
  • OR have a event_end date (custom field) greater or equal than today (if their event_start_date date is less than today's date

The results need to be order using the custom_start field.

I'm working on this code, but I cannot find the relevant documentation for this specific usecase. Can you help, either pointing me to a good online explanation, or explain me how to fix my query?

    $since = date('Y-m-d');
    $args = array(
    'meta_key'          => 'event_date_start_id',
    'category_name'     => 'private',
    'order'             => 'ASC',
    'orderby'           => 'meta_value',
    'meta_query'        => array(
        'relation' => 'OR',
        array(
            //event_date_start_id greater or equal to $since
            'key' => 'bjab_event_date_start_id',
            'value' => $since,
            'type' => 'numeric',
             'compare' => '>='
        ),
        array(
             //event_date_end_id greater or equal to $since
             //and event_date_start_id lower than $since
             // and event_date_start_id != ""
            'relation' => 'AND',
             array(
                'key' => 'bjab_event_date_start_id',
                'value' => $since,
                'type' => 'numeric',
                'compare' => '<'
             ),
             array(
                'key' => 'bjab_event_date_end_id',
                'value' => $since,
                'type' => 'numeric',
                'compare' => '>='
            ),
             array(
                'key' => 'bjab_event_date_end_id',
                'value' => '',
                'type' => 'numeric',
                'compare' => '!='
            )
        )
    )
);

    $query = new WP_Query( $args );

1 Answer 1

1

Since WordPress 4.1 it is possible to build nested meta, date and tax queries. I've not tested it but the code bellow should work. Also, I recommend to replace PHP date() function with current_time from WordPress to get the current date taking in account the blog time zone configuration:

After the chat with you, it seems that you just need to use "DATE" as type argument.

$since = current_time('Y-m-d');
$args = array(
        'meta_key'          => 'bjab_event_date_start_id',
        'category_name'     => 'private',
        'order'             => 'ASC',
        'orderby'           => 'meta_value',
        'meta_query'        => array(
            'relation' => 'OR',
            array(
                //event_date_start_id greater or equal to $since
                'key' => 'bjab_event_date_start_id',
                'value' => $since,
                'type' => 'DATE',
                'compare' => '>='
            ),
            array(
                'key' => 'bjab_event_date_end_id',
                'value' => $since,
                'type' => 'DATE',
                'compare' => '>='
            )
        )
    );

$query = new WP_Query( $args );
11
  • I've updated to 4.1 and tried your suggestion: it shows all posts (not only those in the category "private"). Why is that? I tried also specifying the category as part of each meta value array. Thanks a lot for current_time(), i was unaware of its existence!
    – pixeline
    Dec 23, 2014 at 20:17
  • That is strange. Adding a meta_query shouldn't affect to category parameter. Check that the category slug is correct and try with and without the meta_query. And it is not a good idea include category or any other non-meta query arguments in the meta_query array.
    – cybmeta
    Dec 23, 2014 at 20:48
  • sorry, my mistake: category is fine. Still, the parameters don't seem to matter: all posts in category are displayed, no matter the event_date_start or _end value. I'll update my question with the current code.
    – pixeline
    Dec 23, 2014 at 21:05
  • I think the problem may occur because many posts do not have any event_end_date_id set. I guess the comparison accepts to consider that today >= ''
    – pixeline
    Dec 23, 2014 at 21:07
  • I have updated my question with the current code, in which I tried to address the case where event_end_date_id is empty. No dice: i don't get any result anymore. I'm really having a hard time understanding this syntax.
    – pixeline
    Dec 23, 2014 at 21:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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