I have a custom taxonomy on my site that I query from the sidebar of the currently displayed term. Say I am on the Term X
archives, then my custom post type of Products with the term of Term X
are shown.
I would like to avoid an empty result so I would like to query up the chain until posts are found. For example:
- Term W (5 products)
- Term X (1 product)
- Term Y (0 products)
- Term Z (0 products)
- Term Y (0 products)
- Term X (1 product)
If I were to query Term Z
then I would like to show the result from Term X
and stop there.
Here is what I currently have, but I can only get it to go one level above.
<?php $queryvar = query_posts(array('post_type' => 'event',$the_taxonomy => $the_term,'showposts' => 10 ) ); ?>
<?php if(count($queryvar) < 1) {
do
{
$parents = get_ancestors( $term, $the_taxonomy );
$the_term = $parents[0];
$queryvar = query_posts(array('post_type' => 'event',$the_taxonomy => $the_term,'showposts' => 10 ) );
$term = get_term($the_term, $the_taxonomy);
echo $term->name;
}
while (count ($queryvar) == 0);
}
else { $the_term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
}?>