$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>wp query - Sort posts on custom field AND after that sort on date?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

wp query - Sort posts on custom field AND after that sort on date?

matteradmin8PV0评论

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
Post a comment

comment list (0)

  1. No comments so far