admin管理员组

文章数量:1418657

I'm trying to come up with a query to sort by multiple orderby values. Here's what I have so far:

    $dept_id=2;
    $query=new WP_Query(array(
            'post_type'=>'wpcontactus',
            'nopaging'=>true,
            'post_status'=>array('publish', 'pending', 'future'),
            'meta_key'=>'wcu_dept',
            'meta_value'=>$dept_id,
            'orderby'=>'title',
            'order'=>'ASC'
    ));

I'm trying to query a custom post type, and within that post type, query a meta value.

Then, I'd like to first sort by menu_order ascending, then by a custom meta value wcu_lastname ascending. However, the orderby value didn't seem to be able to take an array.

How can I order the query using multiple orderby values?

I'm trying to come up with a query to sort by multiple orderby values. Here's what I have so far:

    $dept_id=2;
    $query=new WP_Query(array(
            'post_type'=>'wpcontactus',
            'nopaging'=>true,
            'post_status'=>array('publish', 'pending', 'future'),
            'meta_key'=>'wcu_dept',
            'meta_value'=>$dept_id,
            'orderby'=>'title',
            'order'=>'ASC'
    ));

I'm trying to query a custom post type, and within that post type, query a meta value.

Then, I'd like to first sort by menu_order ascending, then by a custom meta value wcu_lastname ascending. However, the orderby value didn't seem to be able to take an array.

How can I order the query using multiple orderby values?

Share Improve this question asked Sep 20, 2012 at 15:50 Force FlowForce Flow 8333 gold badges9 silver badges22 bronze badges 1
  • 3 If I'm not wrong, simply add them space separated. – kaiser Commented Sep 20, 2012 at 16:05
Add a comment  | 

3 Answers 3

Reset to default 37

@Musa how can we put multiple order value for the fields? I was wondering the same question and I found this :

In 4.0, you can now pass an array to WP_Query as the value for orderby.

The syntax looks like:

$q = new WP_Query( array( 
    'orderby' => array( 
       'title'      => 'DESC', 
       'menu_order' => 'ASC' 
    ) 
));

Have a look here for more details : https://make.wordpress/core/2014/08/29/a-more-powerful-order-by-in-wordpress-4-0/

$query=new WP_Query(array(
                'post_type'=>'wpcontactus',
                'nopaging'=>true,
                'post_status'=>array('publish', 'pending', 'future'),
                'meta_query'=>array(
                                array('key'=>'wcu_dept','value'=>$dept_id, 'compare'=>'='),
                            ),
                'meta_key'=>'wcu_firstname',
                'orderby'=>'menu_order wcu_firstname',
                'order'=>'ASC'
        ));

By using what @kaiser suggested and the meta_query option, I was able to get the query I was looking for.

    $args = [
        's'              => $keyword,
        'post_type'      => ['page'],
        'paged'          => $paged,
        'posts_per_page' => PAGE_LIMIT,
        'tax_query' => [
             [
                  'taxonomy'         => TAX_RESOURCE_PAGE,
                  'field'            => 'slug',
                  'terms'            => $c_term_slug
             ],
        ],
        'post_status'    => 'publish',
        'meta_key'       => 'order',
        'meta_type'      => 'NUMERIC',
        'orderby'        => [
             'meta_value_num' => 'ASC',
             'ID' => 'DESC',
        ],
    ];
    $wp_query = new WP_Query( $args );

my code is a example.

本文标签: custom post typesMultiple orderby values in WPQuery