Skip to main content
3 of 5
added 776 characters in body
pixeline
  • 2.9k
  • 7
  • 34
  • 44

How to query using a combination of custom_field values?

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 );
pixeline
  • 2.9k
  • 7
  • 34
  • 44