'Wordpress meta_query order by meta_value then post_date
I am altering a query using the pre_get_posts hook. I have a custom field 'watch_now_url' and i would like posts that have that to appear first then posts without the field afterwards.
I would also like to have the posts in post_date order.
I'm trying:
$query->set( 'meta_query', array(
'relation' => 'OR',
array(
'key' => 'watch_now_url',
'compare' => 'EXISTS'
),
array(
'key' => 'watch_now_url',
'compare' => 'NOT EXISTS'
)
) );
$query->set( 'orderby', ['meta_value' => 'DESC', 'post_date' => 'DESC']);
Posts with the meta_key are appearing first but then nothing is in date order.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
