Support » Themes and Templates » Multi-layer sort (nested loops)

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();

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