WordPress.org

Ready to get started?Download WordPress

Forums

get_categories: hierarchical argument problem (1 post)

  1. evolvd
    Member
    Posted 1 year ago #

    Hi! I've tried searching for quite a while, and haven't found anything that seems to address my issue.

    I have a post structure like this:

    + Category 1
      + Category 2
        + Category 5
          = Post 1
          = Post 2
      + Category 3
        = Post 3
        + Category 6
          = [empty: no posts]
      + Category 4
        = [empty: no posts]

    I want to return the categories that meet the definition of the 'hierarchical' argument: "include sub-categories that are empty, as long as those sub-categories have sub-categories that are not empty"

    When I use:

    $child_categories = get_categories( array(
                                        'parent' => {category1_ID},
                                        'hide_empty' => 1,
                                        'hierarchical' => true
                                    ));

    I only get 'Category 3' returned. It seems 'hide_empty' takes precedence over 'hierarchical'? I would expect to get Category 2 and Category 3.

    I tried:

    $child_categories = get_categories( array(
                                        'parent' => {category1_ID},
                                        'hide_empty' => 0,
                                        'hierarchical' => true
                                    ));

    But I now get Category 2, Category 3, and Category 4. Looks like 'hide_empty' still taking precedence and allowing all categories to be returned.

    I've tried variants of the same code using 1|0 instead of true|false and still not getting what I'd expect.

    I would expect 'hierarchical' to simply modify the effect of 'hide_empty' but it seems to make no difference no matter what value it is... am I doing something wrong?

    How do I get the categories that have non-empty children? ie in my example return Category 2 and Category 3 (and NOT Category 3)

    EDIT:
    I have been digging further into my code, and have determined the behaviour is as expected when I do not specify the 'parent' argument (of course get all non-empty categories from my site)! The codex does not seem to indicate it would make a difference. Perhaps this is a bug?? Has anyone been able to solve this?

Topic Closed

This topic has been closed to new replies.

About this Topic