Title: Query multiple custom fields
Last modified: August 20, 2016

---

# Query multiple custom fields

 *  [Sarnix](https://wordpress.org/support/users/sarnix/)
 * (@sarnix)
 * [14 years, 6 months ago](https://wordpress.org/support/topic/query-multiple-custom-fields/)
 * Hi!
 * I want to get all posts from 1 category and then sort them ascending by date,
   then by time and then by location. Is that possible and how does one do it?
 * So far I find only solutions where you can sort by 1 meta_key. Like in this example:
 *     ```
       $args = array("category_name" => "program", "order" => "ASC", "orderby" => "meta_value", "meta_key" => "event_date");
       $query =  new WP_Query($args);
       ```
   

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

 *  Thread Starter [Sarnix](https://wordpress.org/support/users/sarnix/)
 * (@sarnix)
 * [14 years, 6 months ago](https://wordpress.org/support/topic/query-multiple-custom-fields/#post-2539234)
 * The question wasn’t very clear. So I’ll try it again.
 * In 1 template I want to show all posts of the category program. A post contains
   besides the usual fields some custom values: event_date, event_time, location
   and 2 or 3 more.
    I want to display the posts ordered by event_date, event_time
   and then by location.
 * What function or sql statement do I need to use in order to get an array of posts
   in that specific order?
 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [14 years, 6 months ago](https://wordpress.org/support/topic/query-multiple-custom-fields/#post-2539236)
 * [http://codex.wordpress.org/Function_Reference/query_posts](http://codex.wordpress.org/Function_Reference/query_posts)
   
   [http://codex.wordpress.org/Function_Reference/get_posts](http://codex.wordpress.org/Function_Reference/get_posts)
   [http://codex.wordpress.org/Function_Reference/WP_Query](http://codex.wordpress.org/Function_Reference/WP_Query)
 *  Thread Starter [Sarnix](https://wordpress.org/support/users/sarnix/)
 * (@sarnix)
 * [14 years, 6 months ago](https://wordpress.org/support/topic/query-multiple-custom-fields/#post-2539237)
 * Yes, I read those pages and I found so many other pages on how to search a post
   on meta_keys. But I can’t figure out how to retrieve all posts from a single 
   category already containing all custom values so I can sort them before I loop
   through in the template.
 *  Thread Starter [Sarnix](https://wordpress.org/support/users/sarnix/)
 * (@sarnix)
 * [14 years, 6 months ago](https://wordpress.org/support/topic/query-multiple-custom-fields/#post-2539238)
 * I got it (allthough i can probably leave out the joins). I still would like to
   know how to put the values into the array, but this is the query:
 *     ```
       SELECT DISTINCT wposts.*
               FROM $wpdb->posts wposts
               JOIN $wpdb->postmeta wpostmeta ON wpostmeta.post_id = wposts.ID
               JOIN $wpdb->postmeta wpostmeta2 ON wpostmeta2.post_id = wposts.ID
               JOIN $wpdb->postmeta wpostmeta3 ON wpostmeta3.post_id = wposts.ID
               WHERE wposts.ID = wpostmeta.post_id
               AND wposts.ID = wpostmeta2.post_id
               AND wposts.ID = wpostmeta3.post_id
               AND wpostmeta.meta_key = 'event_date'
               AND wpostmeta2.meta_key = 'event_time'
               AND wpostmeta3.meta_key = 'location'
               ORDER BY wpostmeta.meta_value ASC, wpostmeta2.meta_value ASC, wpostmeta3.meta_value ASC'
       ```
   

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

The topic ‘Query multiple custom fields’ is closed to new replies.

## Tags

 * [custom field](https://wordpress.org/support/topic-tag/custom-field/)
 * [sort](https://wordpress.org/support/topic-tag/sort/)
 * [wp_query](https://wordpress.org/support/topic-tag/wp_query/)

 * 4 replies
 * 2 participants
 * Last reply from: [Sarnix](https://wordpress.org/support/users/sarnix/)
 * Last activity: [14 years, 6 months ago](https://wordpress.org/support/topic/query-multiple-custom-fields/#post-2539238)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
