WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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...)


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

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



This will return:


- 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


- appetizers

- entrees


share|improve this answer

Your Answer


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.