Question

I'm trying to configure a filter navigation where the filters fetched are chosen by the taxonomy 'underkategorier' (Sub categories in Swedish).

The problem I'm having is that I have two pages of which both query different kinds of categories. But for some reason I can't get my list of custom taxonomy links to just list objects within that specific category. One page lists custom posts from Category nr 9 and the other lists custom posts from category 10.

My query is as follows,

<?php $queryname = array(
'post_type'      => 'projekt',
'cat'     => 9,
'order'    => 'ASC',
'posts_per_page' => '-1',); query_posts( $queryname ); ?>

And my taxonomy list of links is as follows,

     <?php $terms = get_terms('underkategorier'); $count = count($terms);  if ( $count > 0 ){
 echo "<div class='post-tags'><a data-filter='all' class='tax-filter filter'>Visa alla</a>";
 foreach ( $terms as $term ) {
   echo "<a data-filter='.$term->slug' class='tax-filter filter'>" . $term->name . '&nbsp;(' . $term->count . ")</a>";      
 }
 echo "</div>";} ?>

So. I want to display links of category 9 + taxonomy 'underkategorier'. Not posts, but terms. For example if 'underkategorier' has a few terms like, 'buildings', 'parks', railways' I want do display these as links. But only if they belong to category 9 as well. So if there where a fourth 'underkategori' by name 'stadiums' which belonged to a category 10 post, this would not show up on the page that displays category 9 posts.

I am still quite new to these coding parts and I could really use some help and would be very grateful. If my question is fuzzy please let me know and I'll try to rephrase. When I get it right I will share it with you all for future help.

Was it helpful?

Solution

This will find all terms in custom taxonomy 'underkategorier' and then will show terms with post from chosen category.

$taxonomies = get_taxonomies(array('name'=>'underkategorier'),'object'); 
foreach($taxonomies as $taxonomy){
    $terms = get_terms( $taxonomy->name, 'orderby=count&hide_empty=0' );
    foreach($terms as $term){
        $wpq = array ('taxonomy'=>'underkategorier','term'=>$term->slug, "cat"=>10);
        $myquery = new WP_Query ($wpq);
        $article_count = $myquery->post_count; 
        echo $term->name.' '.$article_count; //with empty ones
        if ($article_count){
        echo "<ul>";
        echo "<li>".$term->name.' '.$article_count."</li>";
        echo "</ul>";
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top