show all posts of a taxanomy

source : wp-hackers digest # 66,102,1;105,7.

[ A ] Question about showing all posts of a taxonomy, this is a problem I ran into as well, and searched for a long time and found it very hard to figure out.

What I managed to do is list all terms for a taxonomy, and all the posts for each term individually. You may be able to hack this to do what you're trying to do since it is the same as what I needed to do I just didn't have time to make it list only the posts for the taxonomy.

<?php
// Create list of taxonomy terms and list the posts under each term
$post_type = 'post';
$tax = 'your_custom_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 if( function_exists('wp_pagenavi')) { wp_pagenavi(); }
else { ?>
<div class="navigation clearfix">
<div class="alignleft"><?php next_posts_link('&laquo;
Previous Entries') ?></div>
<div class="alignright"><?php previous_posts_link('Next
Entries &raquo;') ?></div>
</div>
<?php } ?>

<?php else : ?>

<h2 class="title">Oops</h2>
<p>Looks like something is missing...</p>

<?php endif; // if have_posts()
wp_reset_query();

} // end foreach #tax_terms
}
?>

[ B ] I was intrigued to find a solution for "taxonomy=clients" and "term=any" where "any" is a wildcard ( WP_Query() supports "any" as a wildcard for post_types but not for terms of a taxonomy. )

Here's a hook that adds an "any" wildcard for terms for a taxonomy assuming some other plugin didn't muck with the " AND 0 " inserted into the WHERE clause when a taxonomy term doesn't match :

add_action('posts_where','my_posts_where',10,2);

function my_posts_where($where,$query) {
if (isset($query->query_vars['taxonomy']) && $query->query_vars['term']=='any') {
global $wpdb;
$tt = $wpdb->term_taxonomy;
$tr = $wpdb->term_relationships;
$where .= $wpdb->prepare(" AND {$wpdb->posts}.ID IN (
SELECT object_id FROM $tr
INNER JOIN $tt ON $tt.term_taxonomy_id = $tr.term_taxonomy_id
WHERE $tt.taxonomy=%s) ",$query->query_vars['taxonomy']);
$where = str_replace(' AND 0 ','',$where);
}
return $where;
}


Your query for this would look like this :

$posts = new WP_Query("post_type=portfolio_item&taxonomy=clients&term=any&posts_per_page=-1");

OR

$posts = new WP_Query(array(
'post_type' => 'portfolio_item',
'taxonomy' => 'clients',
'term' => 'any',
'posts_per_page' => -1,
));

It works but unfortunately it's not very robust because of the need to remove the " AND 0 " from the WHERE clause because there are other reasons get_posts() adds " AND 0 ". If query natively supported "term=any" or at least if there was a hook in /wp-includes/query.php ( lines 2049 and 2068 ) that allowed us to target this specifically it might be better; if any of the core team agree I'll add a ticket to trac.
 
 
Creative Commons License
This work by maniac.vardhan is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.
 
 

0 comments :: show all posts of a taxanomy

Post a Comment