WordPress.org

Ready to get started?Download WordPress

Forums

list custom post type by taxonomy (5 posts)

  1. slee
    Member
    Posted 3 years ago #

    How can i list a custom post type but show them grouped by their taxonomy?
    I know how to show the CPT but I can't figure out how to output the cpt per taxonomy.

  2. osby
    Member
    Posted 3 years ago #

    I have the exact same problem and I've been searching for days now. Can't seem to find anything.

    I get the post type to use the taxonomy.php but when I want to use the menu (which is using the CPT:s hierarchical custom taxonomy) to go from Parent cat -> subCat1 or subCat2 and so on. It displays all post all the time.

    So, I wan't my post type to work like the default post type works.

    Any help would be very appreciated!
    Thanks!

  3. slee
    Member
    Posted 3 years ago #

    Hi osby here is the code I used:

    $post_type = 'post_type_name';
    $tax = 'taxonomy_name';
    $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 class="divisions">'. $tax_term->name . '</h3>';
    	  echo '<ul class="team-members">';
          while ($my_query->have_posts()) : $my_query->the_post(); ?>
             <li>
             <h4 class="team-member"><?php the_title(); ?></h4>
             <?php if(has_post_thumbnail()) { ?>
            <span class="alignleft"><?php the_post_thumbnail(); ?></span>
            <?php } the_content(); ?>
            </li>
            <?php
          endwhile;
    	  echo '</ul>';
        }
        wp_reset_query();
      }
    }

    I hope this helps :)

  4. osby
    Member
    Posted 3 years ago #

    Big thanks to you! I finally got mine working to.

    Great!

  5. SebAshton
    Member
    Posted 2 years ago #

    since 3.1 the way you query taxonomy has changed so for anyone stumbling across this code like i did today here is the amended code:

    [Code moderated as per the Forum Rules. Please use the pastebin]

    enjoy

    S

Topic Closed

This topic has been closed to new replies.

About this Topic