Title: Multi-layer sort (nested loops)
Last modified: August 20, 2016

---

# Multi-layer sort (nested loops)

 *  [billcolumbia](https://wordpress.org/support/users/billcolumbia/)
 * (@billcolumbia)
 * [13 years, 8 months ago](https://wordpress.org/support/topic/multi-layer-sort-nested-loops/)
 * I’ve got a custom post type with 3 meta fields.
 * It’s for displaying upcoming events on our site. I’ve got a: Day, Month and Year
   meta field. I want to first sort posts by year, then by month, then by day. That
   way anytime one of our team members can just add a new event, set the date and
   not have to manually get it in order on the back-end.
 * I tried Nested while loops like so…
 *     ```
       $yearSort = array( 'post_type' => 'events', 'posts_per_page' => -1, 'meta_key' => 'events_year', 'orderby' => 'meta_value');
       					$monthSort = array( 'post_type' => 'events', 'posts_per_page' => -1, 'meta_key' => 'events_month', 'orderby' => 'meta_value');
       					$daySort = array( 'post_type' => 'events', 'posts_per_page' => -1, 'meta_key' => 'events_day', 'orderby' => 'meta_value');
       					$loopHigh = new WP_Query( $yearSort );
       					$loopMid = new WP_Query( $monthSort );
       					$loopLow = new WP_Query( $daySort );
       ```
   
 *     ```
       while ( $loopLow->have_posts() ) : $loopLow->the_post();
       						while ( $loopMid->have_posts() ) : $loopMid->the_post();
       							while ( $loopHigh->have_posts() ) : $loopHigh->the_post();
   
       							endwhile;
       						endwhile;
       					endwhile;
       ```
   

The topic ‘Multi-layer sort (nested loops)’ is closed to new replies.

## Tags

 * [loop](https://wordpress.org/support/topic-tag/loop/)
 * [meta_key](https://wordpress.org/support/topic-tag/meta_key/)
 * [nested](https://wordpress.org/support/topic-tag/nested/)
 * [sort](https://wordpress.org/support/topic-tag/sort/)
 * [wp_query](https://wordpress.org/support/topic-tag/wp_query/)

 * 0 replies
 * 1 participant
 * Last reply from: [billcolumbia](https://wordpress.org/support/users/billcolumbia/)
 * Last activity: [13 years, 8 months ago](https://wordpress.org/support/topic/multi-layer-sort-nested-loops/)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
