How to do WP_Query with two meta fields with orderby clause
Question
The goal is to first try to order by the custom meta field and if its not found then do order by the post date. Here is the query I did so far, but its not working.
$args = array(
'post_type' => 'xyz',
'posts_per_page' => 5,
'meta_query' => array(
'relation' => 'AND',
'query_one' => array(
'key' => 'custom_meta',
'compare' => 'EXISTS',
),
'query_two' => array(
'key' => 'post_date',
),
),
'orderby' => array(
'query_one' => 'DESC',
'query_two' => 'DESC',
),
);
$the_query = new WP_Query( $args );
0
custom-post-types, meta-query, order, wp-query
3 years
2019-10-31T08:47:59-05:00
2019-10-31T08:47:59-05:00 0 Answers
83 views
0
Leave an answer