Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Reading through the order & orderby documentation, it's not clear to me whether there is any support to order based on multiple meta_key values.

Obviously, using meta_query I can return posts based on multiple key-value pairs, but I want to be able to control the order that these results are returned based on multiple meta_keys.

For example, I have pages that have multiple categories and each category has a numerical rank. If a user is searching for pages that are in one of three different categories, I can return all the necessary posts with the following:

$query = array(
  'order' => 'DESC',
  'orderby' => 'meta_value_num',
  'meta_query' => array(
    'relation' => 'OR',
    array( 'key' => 'cat1', 'type' => 'numeric' ),
    array( 'key' => 'cat2', 'type' => 'numeric' ),
    array( 'key' => 'cat3', 'type' => 'numeric' )
  );
);

However, once those are returned, I would like them to be ordered based on the highest numerical value across any one of the categories that results were returned on. In other words, posts with a 9 value in cat1 would appear around the same order as posts with a 9 value in cat3.

Looking at this answer it seems that a meta_key isn't even necessary for 'orderby' => 'meta_value_num', but that doesn't match the documentation for meta_value, which is far more documented than meta_value_num... Any clarification would be useful. Thanks!

share|improve this question
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.