I’m currently working on a WordPress project where I need to display posts with a specific order. My goal is to show not_premium_post posts first, followed by premium_post posts. Additionally, I want all posts to be sorted by date within these categories.
Here is the code I have so far:
$query_args = array(
'post_type' => 'post',
'posts_per_page' => 10,
'post_status' => 'publish',
'meta_query' => array(
'relation' => 'OR',
'premium_post' => array(
'key' => '_premium_post',
'compare' => 'EXISTS',
),
'not_premium_post' => array(
'key' => '_premium_post',
'compare' => 'NOT EXISTS',
),
),
'orderby' => array(
'meta_value' => 'ASC',
'date' => 'DESC',
),
'meta_key' => '_premium_post',
);
The problem I’m encountering is that the posts are not being sorted by date. I need the not_premium_post posts to appear first, followed by the premium_post posts, and all posts should be sorted by date within these categories.
I would appreciate any guidance or suggestions on how to adjust my query to achieve this sorting order. Thank you in advance for your help!
Yahya Qara is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.