0

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',
    ), 

Question

  • That seems to be working, but I am pretty sure this is not the right way to go. Who can help me out?
  • Also, I am wondering why meta_query needs to be there. I only want to query posts based on the tax_query. Is it correct that meta_queryactually does another query of the posts? Is meta_query required in order to sort on multiple values?
  • How can I sort the posts without the CF cc_aff_stick_top by date? Adding 'date' => 'DESC' in the 'orderby' => array is not working.

Answer

Works: 'orderby' => array( 'meta_value_num' => 'DESC', 'date' => 'ASC' )

Works: Setting 'type' => 'NUMERIC' in the meta_query, and 'orderby' => array( 'is_aff' => 'DESC', 'date' => 'DESC' )

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' => 'ASC',
        'is_not_aff' => 'DESC',
    ),
    ) );
    // The Loop
  • Try changing 'is_not_aff' => 'DESC' to 'date' => 'DESC'. That should work. And I think you don't need the is_not_aff in the meta_query.. – Sally CJ Nov 7 '18 at 12:33
  • 1
    Ok, sorry about that - I did not really notice you want to query for posts that have the cc_aff_stick_top meta as well as posts that don't have that meta. And yes we use meta_query to query for multiple meta, and the keys can be used for sorting the results - e.g. the 'is_aff' => 'DESC' in your orderby arg. But if you add the 'date' => 'DESC' into that arg, the posts would/should be sorted by the post date after sorting by the is_aff and is_not_aff. – Sally CJ Nov 7 '18 at 12:52
  • 1
    'orderby' => array( 'meta_value_num' => 'DESC', 'date' => 'ASC' ), works! – RobbTe Nov 7 '18 at 15:02
  • 1
    Setting 'type' => 'NUMERIC', and 'orderby' => array( 'is_aff' => 'DESC', 'date' => 'DESC' ), works as well! – RobbTe Nov 7 '18 at 15:12
  • 1
    Yes, that's right. And no problem, we're here to help each other.. :) All the best. – Sally CJ Nov 7 '18 at 16:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.