Sort posts on custom field AND after that sort on date?

I’m having trouble to understand customizing the new WP_query. I’ve found the script below, partly, here in order to get the sticky posts.
I’ve further edited it in order to:

first get the (sticky) posts with the custom field ‘cc_aff_stick_top’
and order them DESC (high integer – low integer)
Then DESC sort the sticky other posts (without that custom field) based on date.

Especially showing/sorting the second point, I find confusing. I tried it with

‘is_not_aff’ => array(
‘key’ => ‘cc_aff_stick_top’,
‘compare’ => ‘NOT EXISTS’,
),

That seems to be working, but I am pretty sure this is not the right way to go. Who can help me out?

The code:

/* Get all sticky posts */
$sticky = get_option( ‘sticky_posts’ );

//Get term
$term = get_queried_object();

if (!empty($sticky)) {
/* Sort the stickies with the newest ones at the top */
rsort( $sticky );

/* Get the 5 newest stickies (change 5 for a different number) */
$sticky = array_slice( $sticky, 0, 50 );

/* Query sticky posts */
$the_query = new WP_Query( array( ‘post__in’ => $sticky, ‘ignore_sticky_posts’ => 1, ‘post_type’ => ‘ad_listing’, ‘tax_query’ => array(
‘relation’ => ‘OR’,
array (
‘taxonomy’ => ‘ad_cat’,
‘field’ => ‘slug’,
‘terms’ => $term->slug,
),
//Posts with the slug below should be shown everywhere
array (
‘taxonomy’ => ‘ad_cat’,
‘field’ => ‘slug’,
‘terms’ => ‘cc-slug’,
)
),
‘meta_query’ => array(
‘relation’ => ‘OR’,
‘is_aff’ => array(
‘key’ => ‘cc_aff_stick_top’,
‘compare’ => ‘EXISTS’,
),
‘is_not_aff’ => array(
‘key’ => ‘cc_aff_stick_top’,
‘compare’ => ‘NOT EXISTS’,
),
),
‘orderby’ => array(
‘is_aff’ => ‘DESC’,
‘is_not_aff’ => ‘DESC’,
),
) );
// The Loop

Read more here:: Sort posts on custom field AND after that sort on date?

Leave a Reply

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