1

I have the following WP_Query:

$custom_query_args = array(
    'post_type'         => 'mcg_event',
    'posts_per_page'    => -1,
    'meta_query'        => array(
        'relation'  => 'AND',
        array(
            'key'   => 'event_status',
            'value' => 'archived',
        ),
        array(
            'key'       => 'event_start_date',
            'orderby'   => 'meta_value_num',                     
            'order'     => 'ASC',
        )
    ),
);

I'm looking to do 2 things:

  1. only fetch events that have an event_status of archived
  2. order the results by event_start_date

I can do either of these queries separately with no problem but when I put them together as above the order makes no difference.

What am I missing?

1

Change your code with this and might help you with what you are looking for. The issue here is that we cannot include key, order and orderby in meta_query. But if you want to order you result posts based on key than you can pass the query arguments as below:

$custom_query_args = array(
    'post_type'         => 'mcg_event',
    'posts_per_page'    => -1,
    'order'             => 'ASC',
    'meta_key'          => 'event_start_date',
    'orderby'           => 'meta_value_num',
    'meta_query'        => array(
        array(
            'key'   => 'event_status',
            'value' => 'archived',
        ),
    )
);
1

For the ordering you should use the orderby argument of the query. So it would go something like this:

$custom_query_args = array(
    'post_type'     => 'mcg_event',
    'posts_per_page'=> -1,
    'meta_key'      => 'event_start_date',
    'orderby'       => 'meta_value_num',
    'order'         => 'ASC',
    'meta_query'    => array(
        array(
            'key'   => 'event_status',
            'value' => 'archived',
        ),
    )
);

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.