Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a quick question.

Could someone show me how to make a loop that does the following.

Query all posts for one tax (Meal Time: eg. Lunch, dinner, dessert...)

then

Loop each taxonomy (Courses: eg. apps, entrees, desserts...) and display all posts related to that tax.

then continue to loop through until there are no more related taxonomies the the parent "meal time" tax.

share|improve this question
    
What posts do you consider as 'related to that tax'? I can't really understand your question. Do you want to query posts that are filled under both 'lunch' and 'apps' terms? –  Mateusz Hajdziony May 8 '12 at 15:01
add comment

1 Answer

Use get_categories to return the terms in each taxonomy then loop through each getting the posts.

$args = array(
    'taxonomy' => 'meal_time'
);
$meal_times = get_categories( $args );
    foreach ( $meal_times as $meal ) {
            echo '<h3 class="meal-title">' . $meal->cat_name . '</h3>'; //Shows the term name as the title then loops through courses

        $child_args = array(
            'taxonomy' => 'courses'
        );

        $courses = get_categories( $args );
            foreach ( $courses as $course ); {

                echo '<h4 class="course-title">' .$course->cat_name. '</h4>'; //Shows the course then gets the posts

                $new_args = array(
                    'cat' => $meal->term_id,$course->term_id,
                    'posts_per_page' => -1
                );

                    $my_query = new WP_Query( $new_args );

                        while ( $my_query->have_posts () ) : $my_query->the_post ();

                        //Do stuff

                        endwhile;
            }

    }

This will return:

Lunch

- appetizers

All posts assigned with the Lunch Meal Time term and the Appetizers Course Term

- entrees

All posts assigned with the Lunch Meal Time term and the Entrees Course Term

Dinner

- appetizers

- entrees

etc...

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.