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.

Hopefully this is a reasonably straightforward question!

Is it possible to order posts, on the front-end of my WP install, by two meta values?

I have a meta key called genus and another called species. I'd like to sort firstly by genus (ASC), then species (ASC).

Thanks in advance,

share|improve this question

2 Answers 2

up vote 1 down vote accepted

AFAIK, you can't do it using a meta query. Check this example, does what you want.

share|improve this answer
    
You can do this! But it is not simple, read this please: pluginus.net/… –  realmag777 Feb 12 '13 at 20:50

The simplest solution I can think of is to join the posts meta table with posts table twice. Modifying codex example a bit you can use this:

$querystr = "
SELECT wposts.* 
FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta1, $wpdb->postmeta wpostmeta2
WHERE wposts.ID = wpostmeta1.post_id
AND wposts.ID = wpostmeta2.post_id
AND wpostmeta1.meta_key = 'genus'
AND wpostmeta2.meta_key = 'species'
AND wposts.post_type = 'post'
ORDER BY wpostmeta1.meta_value ASC,
         wpostmeta2.meta_value ASC
";
share|improve this answer
    
+1 Thanks, this was way simpler than the link in the accepted answer. –  iambriansreed Jun 29 '12 at 0:04

Your Answer

 
discard

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

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