Title: Sorting posts by multiple metadata information
Last modified: August 19, 2016

---

# Sorting posts by multiple metadata information

 *  [oliverbennett](https://wordpress.org/support/users/oliverbennett/)
 * (@oliverbennett)
 * [16 years, 6 months ago](https://wordpress.org/support/topic/sorting-posts-by-multiple-metadata-information/)
 * Hi there,
 * If anyone could give me any advice with the below that would be great.
 * I have 3 meta keys attached to posts in a specific category (5), they are: day,
   month, year.
 * The month key has a value of 1-12 and I’m ordering the posts by that in the below
   query:
 *     ```
       <?php
       $querystr = "
       SELECT * FROM $wpdb->posts
       LEFT JOIN $wpdb->postmeta ON($wpdb->posts.ID = $wpdb->postmeta.post_id)
       LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id)
       LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
       WHERE $wpdb->term_taxonomy.term_id = 5
       AND $wpdb->term_taxonomy.taxonomy = 'category'
       AND $wpdb->posts.post_status = 'publish'
       AND $wpdb->postmeta.meta_key = 'month'
       ORDER BY $wpdb->postmeta.meta_value ASC
       ";
   
       $pageposts = $wpdb->get_results($querystr, OBJECT);
   
       ?>
       ```
   
 * This works and will output all posts with a month key of 1, then 2 and so on.
   However, when I do this the day key ordering gets messed up, so the individual
   posts for each month will be listed by day as 1,12,2,20,3 etc.
 * I can solve this by changing the last two lines to:
 * AND $wpdb->postmeta.meta_key = ‘day’
    ORDER BY ABS($wpdb->postmeta.meta_value)
   ASC
 * But of course then ALL posts are ordered by day.
 * What I want is to be able to order them by date first *then* sequentially by 
   day.
 * Sorry if the above makes absolutely no sense, I’m rather new to programming and
   SQL.
 * Many thanks,
    Oliver

Viewing 5 replies - 1 through 5 (of 5 total)

 *  [Mark / t31os](https://wordpress.org/support/users/t31os_/)
 * (@t31os_)
 * [16 years, 6 months ago](https://wordpress.org/support/topic/sorting-posts-by-multiple-metadata-information/#post-1277221)
 * You can order by more then one field/value/key…
    [dev.mysql.com/doc/refman/5.0/en/order-by-optimization](http://dev.mysql.com/doc/refman/5.0/en/order-by-optimization.html)
 * Example:
 *     ```
       ORDER by a ASC, b DESC
       ```
   
 * ..and so on..
 *  Thread Starter [oliverbennett](https://wordpress.org/support/users/oliverbennett/)
 * (@oliverbennett)
 * [16 years, 6 months ago](https://wordpress.org/support/topic/sorting-posts-by-multiple-metadata-information/#post-1277224)
 * Hey t31os,
 * Thanks for the suggestion. Sorry, I should have said, I was half-aware of this,
   but I’m unsure how to extract informatiom from two seperate fields/values in 
   the same query.
 * As I understand it the field value is being taken from postmeta.meta_key for 
   use in postmeta.meta_value, so in order to do something like:
 * `ORDER BY ABS('$wpdb->postmeta.meta_value') DESC, day DESC`
 * I’m trying to understand how to access the ‘month’ AND ‘day’. Hope that makes
   sense.
 * Nice kitten, by the way 😀
 *  [Mark / t31os](https://wordpress.org/support/users/t31os_/)
 * (@t31os_)
 * [16 years, 6 months ago](https://wordpress.org/support/topic/sorting-posts-by-multiple-metadata-information/#post-1277243)
 * I think the issue is with your query, you’re only getting one result for each
   post for the meta table using that join..
 * You need a query that joins several meta values.. (As is, you’re selecting either
   the day or month, not both, so you can’t sort by both – because you’re not selecting
   both)
 * Not sure how to approach it, but you’ll likely need a subquery.. SQL is not my
   strongest suit, else i’d offer something up..
 * I’ll say hello to ollie for you (the kitten).. 😉
 *  Thread Starter [oliverbennett](https://wordpress.org/support/users/oliverbennett/)
 * (@oliverbennett)
 * [16 years, 6 months ago](https://wordpress.org/support/topic/sorting-posts-by-multiple-metadata-information/#post-1277441)
 * Ugh, a mixture of poking at this and staring blankly for hours has resulted in
   very little.
 * Also, thinking about it, I need to sort this by year, then month, then date adding
   a further query and yet more complications.
 * If anyone has any input on the best way to approach this or where I could start
   off I’d be very much obliged as right now I’m stumped.
 * Oliver
 *  [Mark / t31os](https://wordpress.org/support/users/t31os_/)
 * (@t31os_)
 * [16 years, 6 months ago](https://wordpress.org/support/topic/sorting-posts-by-multiple-metadata-information/#post-1277452)
 * Sorting is the easy part, essentially what you need is a sub-query, as you’ll
   need multiple queries (single query using sub-query), to grab multiple result
   sets from the meta table, whilst only grabbing unique posts from the post table.
 * I did try to write one, but honestly it’s beyond the scope of my capabilities
   with SQL queries.
 * You could try asking the clever folks on the hackers mailing list, you may get
   more responses there.

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Sorting posts by multiple metadata information’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 5 replies
 * 2 participants
 * Last reply from: [Mark / t31os](https://wordpress.org/support/users/t31os_/)
 * Last activity: [16 years, 6 months ago](https://wordpress.org/support/topic/sorting-posts-by-multiple-metadata-information/#post-1277452)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
