0

I have a query for custom posts and I have managed to order the results alphabetically based on the title first then a 2nd alphabetical ordering of the 1st of 2 meta keys "Species". This works great. But i also want to order the 2nd meta key as well.

<?php 
    $posts = query_posts($query_string . '&meta_key=Species&orderby=title+meta_value&order=ASC'); 
?>

The problem is that is would like to also order by a 2nd meta key called "Variety" so in my mind the query might look something like this but of course it doesn't work.

<?php 
    $posts = query_posts($query_string . '&meta_key1=Species&meta_key2=Variety&orderby=title+meta_value1+meta_value2&order=ASC'); ?>

Further searching has revealed something called meta_query, is this something that may help?

  • Yes meta_query will help and 1. There is not such thing as meta_key1 and meta_key2. 2. Never, ever use query_posts, use WP_Query instead. Specially check multiple orderby section – Robert hue Oct 10 '14 at 20:02
  • Thanks, yes I know no such thing was just trying to illustrate how i thought the solution "might" look. I haven't been able to get WP_Query to work, its a taxonomy-category-name.php page with results from custom posts and I found the above query did hence its use. – Bradz Oct 10 '14 at 20:20
  • I cant test because of the WP Query issue but is this going in the right direction? Sort alphabetically by title then 2 key words. $args = array( 'post_type' => 'plant-catalogue', 'meta_query' => array( array( 'key' => 'Species', 'key' => 'Variety', 'order' => 'ASC', ), ), 'orderby' => 'title meta_query', 'order' => 'ASC', ); $query = new WP_Query( $args ); – Bradz Oct 10 '14 at 20:36

Your Answer

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

Browse other questions tagged or ask your own question.