How to sort multiple wordpress custom field values?
Asked Answered
B

4

7

Display posts with 'Product' type ordered by 'Price' custom field:

$query = new WP_Query( 
                      array ( 'post_type' => 'product', 
                              'orderby' => 'meta_value', 
                              'meta_key' => 'price' ) 
                     );

Which code should I use if also want to order by 'Size'?

Another example on which I need multiple sort on custom fields:

Display posts with 'Event' type ordered by 'Start_Hour' and then by 'Start_Minute'.

Brede answered 1/10, 2012 at 19:21 Comment(0)
B
9

Thanks to Bainternet I found the solution:

function orderbyreplace($orderby) {
    return str_replace('menu_order', 'mt1.meta_value, mt2.meta_value', $orderby);
}

and...

$args = array(
  'post_type'=>'Events',
  'orderby' => 'menu_order',
  'order' => 'ASC',
  'meta_query' => array(
        array(
            'key' => 'Start_Hour',
            'value' => '',
            'compare' => 'LIKE'
        ),
        array(
            'key' => 'Start_Minute',
            'value' => '',
            'compare' => 'LIKE'
        )
    )
);

add_filter('posts_orderby','orderbyreplace');
$loop = new WP_Query( $args );
remove_filter('posts_orderby','orderbyreplace');
Brede answered 7/10, 2012 at 20:36 Comment(1)
it seems this does not work because first the number rows found is queried. SELECT SQL_CALC_FOUND_ROWS zmw_posts.ID FROM Logarithm
S
2

I think this changed slightly in Wordpress 3.7.

I had to change

return str_replace('menu_order', 'mt2.meta_value, mt1.meta_value', $orderby);

into

return str_replace('wp_posts.menu_order', 'mt2.meta_value, mt1.meta_value', $orderby);

Thanks for the initial solution! [Edited]

Serval answered 24/11, 2013 at 23:46 Comment(0)
T
1

Here's an example of using more than one meta_key and orderby that I believe should work:

$params = array(
'post_type' => 'product',
'meta_query' => array(
    array(
            'key' => 'price',
            'value' => '',
            'compare' => 'LIKE'
    ),
    array(
            'key' => 'size',
            'value' => '',
            'compare' => 'LIKE'
    )
),
'orderby' => 'price size',
'order' => 'ASC'
);
$query = new WP_Query;
$resulting_obj = $query->query($params);

You'll need to play with the meta_query items a bit more, especially the 'value' parameter. Please have a good look at http://codex.wordpress.org/Class_Reference/WP_Query in the 'Custom Field Parameters' section.

Trivium answered 2/10, 2012 at 9:49 Comment(0)
G
-1

You don't need any filter or hooks to sort multiple custom fields, if your one of custom field is meta_key and other one is normal column of table, than use these parameters/arguments in your query.

'meta_key' => 'KEY_NAME',
'orderby' => 'meta_value_num SECOND_COLUMN_NAME',
'order' => 'ASC' or 'order' => 'DESC'

Here the order of meta_value_num and SECOND_COLUMN_NAME matter, you may see different-2 result based on the order.

Grodno answered 6/2, 2015 at 13:16 Comment(1)
This will sort by KEY_NAME only. The only way this orderby will function is if SECOND_COLUMN_NAME is not a custom field, which fails to answer this questions.Egan

© 2022 - 2024 — McMap. All rights reserved.