$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 - Order 2 meta_queries differently in WP_Query?|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 - Order 2 meta_queries differently in WP_Query?

matteradmin9PV0评论

I have a custom WP_Query with 2 different meta queries. I want to order the results of both queries differently. The first meta_query I want to order by a custom field (high integer - low integer). The second meta_query I want to order by date. To be clear, I want to first show the results from the first query and then the results from the second query. Below the code I currently have. For some reason it is ordering both meta_query by meta_value_num. How can I order both meta queries differently, within the same WP_Query? Or do I actually have to make 2 different WP queries?

<?php
$sticky = get_option( 'sticky_posts' );
$term = get_queried_object();
/* Query sticky posts */
$the_query = new WP_Query( array( 'post__in' => $sticky, 'ignore_sticky_posts' => 1, 'posts_per_page' => 5, 'post_type' => 'ads', 'tax_query' => array(
    //Get posts by term slug
    'relation' => 'OR',
        array (
            'taxonomy' => 'ad_cat',
            'field' => 'slug',
            'terms' => $term->slug,
        )
    ),
    'meta_query' => array(
   //Get posts with custom field key 'cc_aff_stick_top'
    'relation' => 'OR', 
    'is_aff' => array(
        'key' => 'cc_aff_stick_top',
        'compare' => 'EXISTS',
    ),
    //Get posts without key
    'is_not_aff' => array(
        'key' => 'cc_aff_stick_top',
        'compare' => 'NOT EXISTS',
    ), 
),
//Order by CF cc_aff_stick_top and otherwise by date
'orderby' => array( 
    'meta_value_num' => 'ASC',
    'date' => 'DESC'
),
) );
// The Loop
if ( $the_query->have_posts() ) {
Post a comment

comment list (0)

  1. No comments so far