I want to query by 1 meta key, but sort by another key that may or may not exist, and then by post date
So the end result would be something like:
Meta Key A / Date 1
Meta Key A / Date 2
Meta Key B / Date 1
No Meta Key / Date 1
No Meta Key / Date 2
Query args like the following are not returning any posts:
$customer_orders = get_posts( array(
'numberposts' => $order_count,
'meta_query' => array(
array(
'key' => '_customer_user',
'value' => get_current_user_id(),
'compare' => '=',
),
array(
'key' => '_dealer_number',
'compare' => 'EXISTS',
),
array(
'key' => '_dealer_number',
'compare' => 'NOT EXISTS',
),
'relation' => 'AND'
),
'orderby' => 'meta_value date',
'order' => 'ASC'
) );
Is this possible with WP_Query
args or do I need to filter posts_orderby
? If so, what might I use?
_dealer_number
exists and at same time not exists: it's pretty impossible :) Maybe usingOR
as relation may work, but I'm not sure and moreover in that case_customer_user
becomes optional too, that doesn't seems what you want. I'm afraid you need to filterposts_orderby
,posts_where
andposts_join
to obtain desired result._dealer_number
existing and not existing at the same time is impossible. :) But I still want_customer_user
to be required at all times. Is there a way to filterposts_orderby
, etc so that the custom orderby clause only effects thisget_posts()
query?get_posts( array( 'filter_me' => TRUE
and inside the filter chek if flag exists, if not, don't filter. And when you apply the filter, immediately remove it so that it run once.