5

I am trying to order blog posts by 'city' first and then order by 'street_name' within each city. I can't seem to get the 'street_name' to display in alphabetical order. I am using WP_Query:

                <?php
                $args = array(
                    'post_type'=> 'property',
                    'meta_query' => array(
                                array(
                                    'relation' => 'AND' ,
                                    array(
                                        'meta_key' => 'city',
                                        'orderby' => 'meta_value',
                                        'order' => 'ASC'
                                    ),
                                    array(
                                        'meta_key' => 'street_name',
                                        'orderby' => 'meta_value',
                                        'order' => 'ASC'
                                    ),
                                )

), Any ideas?

7

meta_query and orderby are seperate parameters, you just put them together in an array. You will have to do one then the other.

e.g.

<?php
$args = array(
    'post_type'     => 'property',
    'meta_query'    => array(
        array(
            'relation' => 'AND',
            'city_clause' => array(
                'key'       => 'city',
                'compare'   => 'EXISTS',
            ),
            'street_clause' => array(
                'key'       => 'street_name',
                'compare'   => 'EXISTS',
            ),
        )
    )
    'orderby' => array(
        'city_clause'       => 'desc',
        'street_clause'     => 'desc',
    )
)
?>

https://developer.wordpress.org/reference/classes/wp_query/#order-orderby-parameters

| improve this answer | |
  • Thanks @Nath ! I did try this and I still can't seem to get the street name to order alphabetically... – bhood Dec 21 '16 at 13:43
  • @bhood Okay I have updated my answer to use explicit array indexes that are referenced by the orderby parameter. See if that works. – ngearing Dec 21 '16 at 23:05
3

Took me awhile to figure out how to order by numeric values with multiple meta keys since you can't just use orderby meta_value_num. Instead you set the type to numeric in the meta query. This is working production code.

    $meta_query = array(
        'relation' => 'AND',
        'query_one' => array(
            'key' => '_rama_ads_type'
        ),
        'query_two' => array(
            'key' => '_rama_ads_order',
            'type' => 'NUMERIC',
        ),
    );

    $order_by = array(
        'query_one' => 'ASC',
        'query_two' => 'DESC',
    );

    $query->set( 'meta_query', $meta_query );
    $query->set( 'orderby', $order_by );
| improve this answer | |
1

You need to order by specific clauses for meta_query.

This link should cover everything to help you sort out the syntax and get it working.

https://make.wordpress.org/core/2015/03/30/query-improvements-in-wp-4-2-orderby-and-meta_query/

| improve this answer | |
-2

Try this, it might help.

<?php
$args = array(
'post_type'  => 'Sports',
'meta_query' => array(
    array(
        'key'     => 'league_count',
                    'orderby' => 'meta_value_num', /* use this only 
                             if the key stores number otherwise use 'meta_value' */
                    'order' => 'ASC'
    ),
    array(
        'key'     => 'matches_count',
                    'orderby' => 'meta_value_num',
                    'order' => 'ASC'
    ),
),
);
$query = new WP_Query( $args );
?>
| improve this answer | |
  • meta_query doesn't support orderby arguments like this. – Jacob Peattie Jun 1 '18 at 14:05
  • Please edit your answer, and add an explanation: why could that solve the problem? – fuxia Jun 1 '18 at 16:00

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.