12
List Posts For Terms Of A Custom Taxonomy For Any Post Type
I am using this on a custom page-slug.php template to show taxonomy posts listed by the terms of the taxonomy that aren’t empty.
It could be very useful, and easily manipulated.
<div id="content-main">
<?php
// List posts by the terms for a custom taxonomy of any post type
$post_type = 'YOUR_POST_TYPE';
$tax = 'YOUR_TAXONOMY';
$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() ) : ?>
<h2 class="breadcrumb">All <?php echo $tax; ?> Posts For <?php echo $tax_term->name; ?></h2>
<ul class="taxlist">
<?php while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
<li id="post-<?php the_ID(); ?>">
<a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
</li>
<?php endwhile; // end of loop ?>
</ul>
<?php else : ?>
<?php endif; // if have_posts()
wp_reset_query();
} // end foreach #tax_terms
}
?>
</div>






User Comments
( ADD YOURS )Daedal July 30
Thank you for this great tip :) One more Q. How could one automatically create links for every taxonomy in title? IE:
All Posts For <a href="how_to_get_tax_term_link_here" title="name; ?>">name; ?>
Cheers and keep up the good work!
Daedal July 31
Ok I figure it out myself, dunno if it's best solution but here we go:
We simply receive term slug and then construct whole url:
All < a href="//slug; ? >/" rel="bookmark" title="Link for name; ?>">name; ? >
Above version will probably be stripped out from tags so let's try with html entities :)
<h3 class="breadcrumb">All <a href="//slug; ?>/" rel="bookmark" title="Link for name; ?>">name; ?></a></h3>
and if all that fails here is description:
So the goal was to construct link for each taxonomy term just like category.
I did it like this:
slug;"> and that would work.
Daedal July 31
Ah shuu, it didn't work, everything was stripped :(
Ok construct link out of bloginfo('url'); then / then echo $tax; then / then echo $tax_term->slug; then /
So in the end link href would look like this:
bloginfo('url'); / echo $tax; / echo $tax_term->slug; /
Slashes are between php tags
Jared July 31
It should work, I use this exact code with my custom taxonomy, and the 'post' post type, on a theme I made. It should list everything like this if there are posts for these pretend terms of a taxonomy called um, Test
-- All Test posts for Testterm1
---- a Testterm1 post
---- a Testterm1 post
---- a Testterm1 post
-- All Test posts for Testterm2
---- a Testterm2 post
---- a Testterm2 post
---- a Testterm2 post
-- All Test posts for Testterm3
---- a Testterm3 post
---- a Testterm3 post
---- a Testterm3 post
...and so on. Did you use a custom post type that was hierarchical, or like the 'page' post type? I don't know how it would work for something like that. But should work fine with a non-hierarchical type like 'post'.
Daedal July 31
Hey Jared. Yeah it is hierarchical and I mean really hierarchical. Up to few levels deep. But wp always creates url's like http://new2wp.com/music/last-tax-term/ No matter if I have ie music -> techno -> house -> ambient, it will always be just the last term ie in above example would be http://new2wp.com/music/ambient/ and that is fine (at least in my case). I've had problem with music item cause term name didn't show in item permalink so I found this nice hack http://xplus3.net/2010/05/20/wp3-custom-post-type-permalinks/
and now posts look like this http://new2wp.com/music/ambient/music-item if we have tax music with tax term ambient
Daedal July 31
Hey Jared. Yes it is hierarchical and I mean really hierarchical :) Few levels deep and WP is generating permalinks always just with last tax term (last child). Ie we have tax music -> techno -> house -> ambient and link would be http://new2wp.com/music/ambient So this solution works pretty well.
Cheers
Wander Lima August 31
Really usefull, tks man!
Wander Lima August 31
tks man, usefull code
so November 3
Thx !
DWP March 29
This works really well, but how would i use this to display items for a taxonomy and not individual taxonomy names. So if i have custom post types organized for the following taxonomy:
Brands
- Calvin Klein
- Prada
I want to show items under "Brands" and not "Calvin Klein" etc etc.
David Hedley June 13
Excellent script, however, I would appreciate some help with applying this the following hierarchical setup. I have 2 top level terms, one has 3 subcategories and 1 is top level only.
I need to show:
TOP LEVEL TERM 1 TITLE
> SUBCAT 1 TITLE
>> POST LIST
> SUBCAT 2 TITLE
>> POST LIST
> SUBCAT 3 TITLE
>> POST LIST
TOP LEVEL TERM 2 TITLE
(NO SUBCATS)
>> POST LIST
I.E. INJECT THE TOP LEVEL TERM DISPLAY NAME AT THE CORRECT POINT
Thanks!
Apollo July 28
This function is perfect - thank you.
If anyone is interested, i was looking for away to exclude certain taxonomy terms. I was able to accomplish this using 'tax_query' ... To do this i added the following to the WP_query array:
$args = array(
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => '--taxonomyname--',
'field' => 'slug',
'terms' => $tax_term->slug,
),
array(
'taxonomy' => '--taxonomyname--',
'field' => 'id',
'terms' => array( 21, 09, 32 ),
'operator' => 'NOT IN',
)
),
'post_type' => $post_type,
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1
);
Trackbacks