WP_Query orderby custom field then post date in one query

I need to order the first three posts by a custom field, and subsequent posts by post_date in one query – for example:

Post 4 (field_order: 1)
Post 3 (field_order: 2)
Post 2 (field_order: 3)
Post 1 (field_order: null)
Post 0 (field_order: null)

Post 1 and Post 0 need to be ordered by post_date

I can order by custom field easily enough but when I add a post without the custom field (Post 1 & 0) it doesn’t appear in the query.

Below is the query I am currently trying:

$args = array(
‘post_type’ => ‘insights’,
‘posts_per_page’ => ‘9’,
‘meta_query’ => array(
array(
‘key’ => ‘field_order’,
‘value’ => ‘0’,
‘compare’ => ‘>’
)
),
‘orderby’ => array(
‘field_order’ => ‘ASC’,
‘post_date’ => ‘ASC’,
),
‘order’ => ‘ASC’
);

Any ideas how to achieve this would be much appreciated.

Read more here:: WP_Query orderby custom field then post date in one query

Leave a Reply

Your email address will not be published. Required fields are marked *