• Resolved Ayman Aboulnasr

    (@ayman3011)


    Hello fellow WordPress people ??

    Here’s the scenario,

    I have a custom post type called: staff_members
    Custom post types under that could be names of people with their biographies.
    I also have a custom taxonomy called: departments (highlighted in bold below)

    How do I output the following?

    Quality Control Department

    • John Smith
    • David Feldman

    Financial Department

    • Mark Robinson
    • Richard Carpenter
    • Sarah Fisherman

    Media Department

    • James Shelver
    • Alfred Kane

    I’m guessing It will have to be a nested loop. The outer loop to output the custom taxonomies values (in bold above).
    Then the inner loop will output the custom posts that belong to each taxonomy (the staff members names above)

    So how do I do that? Is there such a loop?

    Thank you for taking the time to read and respond.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter Ayman Aboulnasr

    (@ayman3011)

    After a bit of hunting around for a solution, here’s what I’ve found and worked like a charm!

    //for a given post type, return all taxonomies attached to it
    $post_type = 'staff_members';
    $tax = 'departments';
    $tax_terms = get_terms($tax);
    if ($tax_terms) {
      foreach ($tax_terms  as $tax_term) {
        $args=array(
          'post_type' => $post_type,
          "$tax" => $tax_term->slug,
          'post_status' => 'publish',
          'posts_per_page' => -1,
          'caller_get_posts'=> 1
        );
    
        $my_query = null;
        $my_query = new WP_Query($args);
        if( $my_query->have_posts() ) {
          echo '<h3>'.$tax_term->name.'</h3>';
          while ($my_query->have_posts()) : $my_query->the_post(); ?>
            <a href="<?php the_permalink();?>"><?php the_title();?></a>
            <?php
          endwhile;
        }
        wp_reset_query();
      }
    }
    ?>

    I hope this will help everybody that faces the same situation I had.

    Thanks for posting this info.

    I am currently looking for a way to make a nested loop that shows only the other custom posts with in the same custom taxonomy of the post.

    So for instance, if you had a custom post type called “Events” and a Custom Taxonomy called “Type”. Then say one of those events was called “Birthday” how would you show a list of all the other “Birthday Events” on that single post page?

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How do I output Custom Taxonomies with the custom posts that belong to them?’ is closed to new replies.