Title: Sarnix's Replies | WordPress.org

---

# Sarnix

  [  ](https://wordpress.org/support/users/sarnix/)

 *   [Profile](https://wordpress.org/support/users/sarnix/)
 *   [Topics Started](https://wordpress.org/support/users/sarnix/topics/)
 *   [Replies Created](https://wordpress.org/support/users/sarnix/replies/)
 *   [Reviews Written](https://wordpress.org/support/users/sarnix/reviews/)
 *   [Topics Replied To](https://wordpress.org/support/users/sarnix/replied-to/)
 *   [Engagements](https://wordpress.org/support/users/sarnix/engagements/)
 *   [Favorites](https://wordpress.org/support/users/sarnix/favorites/)

 Search replies:

## Forum Replies Created

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

 *   Forum: [Themes and Templates](https://wordpress.org/support/forum/themes-and-templates/)
   
   In reply to: [Query multiple custom fields](https://wordpress.org/support/topic/query-multiple-custom-fields/)
 *  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'
       ```
   
 *   Forum: [Themes and Templates](https://wordpress.org/support/forum/themes-and-templates/)
   
   In reply to: [Query multiple custom fields](https://wordpress.org/support/topic/query-multiple-custom-fields/)
 *  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.
 *   Forum: [Themes and Templates](https://wordpress.org/support/forum/themes-and-templates/)
   
   In reply to: [Query multiple custom fields](https://wordpress.org/support/topic/query-multiple-custom-fields/)
 *  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?

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