2

I have a category called News and a custom Taxonomy called Filters which is a hierarchical Taxonomy.

When a user creates a Post, they select a subcategory under news and a sub-filter under the Filter Taxonomy.

Now I am trying to list all the 'Sub-Categories' and 'Filters' when a user navigates to /news.

Listing the Sub-Categories for the 'News' category was easy. But I can't seem to figure out a way to list all the 'Sub-Filters' for the Taxonomy 'Filters' limited to only the category 'News'.

Here is the code I used for getting a list of sub-categories for the category news:

function get_child_categories()
{
    if (is_category()) {
        $thiscat = get_category(get_query_var('cat'));
        $catid = $thiscat->cat_ID;
        $args = array('parent' => $catid);
        $categories = get_categories($args);

        return $categories;
    }

    return false;
}

I was hoping for a similar function which will list all terms for taxonomy 'Filters' but only limited to the category 'News'. Here is a screenshot of what I am trying to achieve:

In the screenshot below '/news' is related to the 'News' category in the admin area. So if a user goes to /news, the front end should list all the posts for the 'News' Category.

The page should also list all the sub-categories under the Category 'News'. This is done as you can see from the horizontal list of Sub Categories.

Now the user can also select a Filter as can be seen in the admin UI. What I am trying to achieve is to list all the Filters for any post that may be categorized under 'News' and display in the horizontal list where under 'Filters'. This will then be used to filter the posts when the user clicks for example 'World News' to list only the posts that have the Filter 'World News' checked.

enter image description here

Current Category/Taxonomy in the Admin Area when editing the post

enter image description here

  • 1
    Could you please share the screenshot from admin area to show how you are relating categories and taxonomies? – Vinod Dalvi Apr 20 '17 at 11:41
  • @VinodDalvi Just added the admin area on how it looks with the Categories and Taxonomy – Niraj Pandey Apr 20 '17 at 11:50
  • Ok Thank you but still i am not getting what you meant by "I was hoping for a similar function which will list all terms for taxonomy 'Filters' but only limited to the category 'News'." so could you please describe it a bit more in detail? – Vinod Dalvi Apr 20 '17 at 11:52
  • @VinodDalvi I have added further detail which will hopefully clarify what I am trying to achieve. – Niraj Pandey Apr 20 '17 at 13:10
  • I understood your requirement now and provided the solution. – Vinod Dalvi Apr 21 '17 at 7:57
3

The following code will do it. Please change the 'filter' text in the below code to whatever filters taxonomy name you have set.

if(is_category() ){

    $thiscat = get_queried_object_id(); 
    $filter_tax = array();
    $args = array( 'category' => $thiscat );
    $lastposts = get_posts( $args );

    foreach ( $lastposts as $post ){
        setup_postdata( $post );
        $terms = get_the_terms( $post->ID, 'filter' ); // Change the taxonomy name here

        if ( $terms && ! is_wp_error( $terms ) ){

         foreach ( $terms as $term ) {
            $filter_tax[] = $term;
         }

        }
    }
    wp_reset_postdata();

    if( !empty($filter_tax) ){
        print_r($filter_tax);
    } else {
        echo 'No filter set.';
    }

}
1

I'm not sure that I've understand what you are asking for but i hope this can help you out-

Getting all sub-categories of current category

$args = array('parent' => 17); // Or get queried object for ID
$categories = get_categories( $args );

Getting all sub-categories from all levels

$args = array('child_of' => 17); // Or get queried object for ID
$categories = get_categories( $args );

Getting all categories under custom taxonomy

$terms = get_terms(array(
    'taxonomy' => 'post_tag',
    'hide_empty' => false,
));
  • I have added further info to clarify what I am trying to achieve. In your answer, I am trying to get the terms for taxonomy 'filters' but only for the current category. So essentially, I would replace the 'post_tag' with 'filters' but I also need to further limit the terms by the current category (in the example above, it would be a category called 'news') – Niraj Pandey Apr 20 '17 at 13:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.