In my WordPress categories section, I've a Food main category, with other sub-categories ("hamburger, pasta, main, etc"). In post page i want to show only the child categories that are filled in the post.

I want to show only child category selected. If the post is in Food > hamburger and Food > main, i want to show only these 2 categories on post page, instead of all categories. I tried with this code, but it will show all categories:

$categories = get_categories( array(
    'taxonomy' => 'category', 
    'orderby' => 'name',
    'order' => 'ASC',
    'hide_empty' => true, 
    'include' => 'all',
    'exclude' => '', 
    'exclude_tree' => 'all', 
    'number' => false,
    'fields' => 'all',
    'name' => '',
    'slug' => '',
    'hierarchical' => true,
    'search' => '',
    'name__like' => '',
    'description__like' => '',
    'pad_counts' => false,
    'get' => '',
    'child_of' => false,
    'childless' => false,
    'cache_domain' => 'core',
    'update_term_meta_cache' => true,
    'meta_query' => '',
    'meta_key' => array(),
    'meta_value'=> ''

foreach ( $categories as $category ) {
    printf( '<a href="%1$s">%2$s</a><br />',
        esc_url( get_category_link( $category->term_id ) ),
        esc_html( $category->name )

1 Answer 1


You should use get_the_category() instead of get_categories().

The get_the_category() function will return only categories assigned to the post, while get_categories() that you use returns all existing categories.


To get categories assigned to post, but only those with the ancestor Food, you can use code:

$args = [
    'object_ids' => get_the_ID(),   // only categories assigned to current post
    'child_of' => {ID_of_food_cat}  // limit results to children (descendants) of this category
$cats = get_categories($args);

ID of parent category can be hard-coded or dynamic:

$food_cat_id = get_category_by_slug('food');  // <-- slug of Food
$food_cat_id = ($food_cat_id instanceof WP_Term) ? $food_cat_id->term_id : 0;
  • ok this work, but it will show all categoryes of post, but if i want to filter only child categories from "food" ? because it will return all categoryes that i've selected, i need some not all. Thanks!
    – davi90ct
    May 3, 2019 at 14:43
  • Do you want to skip all parents, keeping only children on the last level? Sample hierarchy: Food > Cat.A > Cat.A1, Food > Cat.B > Cat.B1, Food > Cat.B > Cat.B2. If post has selected (assigned) categories Cat.A, Cat.A1, Cat.B2, Food, then only Cat.A1 and Cat.B2 should be displayed. Right?
    – nmr
    May 3, 2019 at 15:08
  • Not properly, i have Food ( main category ) and under it i've childs ( "hamburger, pasta, main, etc"). I need to display only childs of "Food" Category. The final result is "hamburger, pasta, main". Now i've all categories and child ( Food, Pasta, Beverage, Main, Second, Vegetable etc ). I need only to show all child's of "Food" main category. THanks
    – davi90ct
    May 3, 2019 at 15:18
  • @davi90ct I updated the answer
    – nmr
    May 3, 2019 at 20:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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