WordPress.org

Ready to get started?Download WordPress

Forums

Multi-layer sort (nested loops) (1 post)

  1. billcolumbia
    Member
    Posted 2 years ago #

    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;

Topic Closed

This topic has been closed to new replies.

About this Topic