• I have a custom post type that uses the built in category taxonomy and two custom taxonomies. All three of the taxonomies are hierarchical, having children and grandchildren. For example:

    Custom Post Type: Recipe

    Categories: Everyday, Travel, Holiday

    Sub-categories:
    Everyday => none,
    Travel => camping, road trip, destination
    Holidays => easter, thanksgiving, xmas

    Custom Taxonomy #1 (Diet): Paleo, Vegan, Gluten-Free
    Tax children #1:
    Paleo => grain-free, grass-fed
    Vegan => raw, fruitarian
    Gluten-free => none

    Custom Taxonomy #2 (Cuisine) : American, Asian, African
    Tax children #2:
    American => southern, pub
    Asian => chinese, thai, indian
    African => ethiopian

    *Some categories and taxonomies will naturally have many children (and grandchildren) while some will have none.

    GOAL:
    If a Recipe post is under any category and taxonomy that’s a parent, what we want is to display in the footer, a list of links that’re the result of child categories x child diets x child cuisines. For example:

    Let’s say that you’re on a recipe post with the following attributes…

    Recipe post title: Quick Asian Vegan Meals For Holidays
    category: Holidays
    diet: Vegan
    cuisine: Asian

    Because this recipe post is filed under categories and taxonomies that have children, what we want is to show a list that’s 3 sub-categories x 2 sub-diets x 3 sub-cuisines, totaling 18 links, like this…

    Quick Chinese Raw Vegan Meals For Easter
    Quick Thai Raw Vegan Meals For Easter
    Quick Indian Raw Vegan Meals For Easter
    Quick Chinese Fruitarian Meals For Easter
    Quick Thai Fruitarian Meals For Easter
    Quick Indian Fruitarian Meals For Easter

    Quick Chinese Raw Vegan Meals For Thanksgiving
    Quick Thai Raw Vegan Meals For Thanksgiving
    Quick Indian Raw Vegan Meals For Thanksgiving
    Quick Chinese Fruitarian Meals For Thanksgiving
    Quick Thai Fruitarian Meals For Thanksgiving
    Quick Indian Fruitarian Meals For Thanksgiving

    Quick Chinese Raw Vegan Meals For Xmas
    Quick Thai Raw Vegan Meals For Xmas
    Quick Indian Raw Vegan Meals For Xmas
    Quick Chinese Fruitarian Meals For Xmas
    Quick Thai Fruitarian Meals For Xmas
    Quick Indian Fruitarian Meals For Xmas

    We want a list of links to show in the footer always, even if a recipe post is filed under any cat or tax that is not a parent.

    So let’s say you have a different recipe post with the same attributes as the previous example, except now we swap Vegan (who’s a parent) with Gluten-free (who is not a parent). For example:

    Recipe post title: Quick Asian Gluten-free Meals For Holidays
    category: Holidays
    diet: Gluten-free
    cuisine: Asian

    Now we have 3 sub-categories x 0 sub-diets x 3 sub-cuisines, totaling 9 links. The resulting combo of links will now look like this…

    Quick Chinese Gluten Free Meals For Easter
    Quick Thai Gluten Free Meals For Easter
    Quick Indian Gluten Free Meals For Easter

    Quick Chinese Gluten Free Meals For Thanksgiving
    Quick Thai Gluten Free Meals For Thanksgiving
    Quick Indian Gluten Free Meals For Thanksgiving

    Quick Chinese Gluten Free Meals For Xmas
    Quick Thai Gluten Free Meals For Xmas
    Quick Indian Gluten Free Meals For Xmas

    We have a foreach code snippet that will do this for us, BUT it will only work on recipe posts that have a parent cat or tax. If the recipe post doesn’t have either a parent cat or tax (like the Gluten-free example above), no list will show.

    ALSO, the foreach code snippet shows ALL children and grandchildren of categories and taxonomies, making the list of links very very long. Ideally, we want the list to show ONLY the direct, immediate children, and not grandchildren.

    I’m thinking we should maybe not use a foreach function and just query posts instead…hm. What do you think?

    HUGE THANK YOU for those who’ve read up to this point and want to step up to the plate and help. Thank you so much in advance for all your help and patience! ??

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    Thanks for all the examples, it helps a lot in understanding your goals. I think I can summarize succinctly. You want all permutations of immediate taxonomy term children. I hope that’s correct, apologies if not. Either way, I think I have the solution, though it may need adjustments if I’ve misunderstood.

    Any kind of permutation development needs a nested, re-entrant control structure, but because some terms may be lacking children, foreach is inappropriate because the nested loops will not execute when there are no terms. As you can imagine, WP often encounters this situation. Thus there is a specific core class to handle such situations — Walker. The class “walks” all the possible paths of any arbitrary tree structure, generating the results of each “walk”. See the Used By section of the linked doc page to see how this class is extended for specific uses by WP. You’ll be doing something similar.

    The other important part of this puzzle is getting the proper terms that generate the permutations. Use get_terms() for this. You can pass elaborate WP_Term_Query arguments so you only get the terms you desire. In your case, you want to use the “parent” argument (plus the proper taxonomy of course). When you supply the current term ID as the parent value, the function will only return term objects that are immediate children, no grand children or lower terms will be included.

    Armed with all the various child terms to drive the walker, all possible permutations can be generated. Not only the proper human readable link text, but the proper link URL parameters needed for each permutation as well.

    If you’ve not worked with similar walkers before, it’ll take some study to understand what needs to happen. The Used By source code is a useful reference for examples. I’d suggest building a custom page template as a test platform for learning how to use this very useful class. It can be rather confusing at first, but it’s a very powerful and useful class, so well worth taking the time to understand it.

    If you have any further questions or need help in understanding, just ask!

    Thread Starter RoselleCelina

    (@rosellecelina)

    Yowza! Thanks for reading my post and providing an equally long and thoughtful reply. ??

    I’ll take your advice, go through the links you provided and play around with some code.

    Thanks a bunch for pointing me in the right direction, you’re awesome.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Show list of SUB-CATEGORIES x CUSTOM TAXONOMY 1 children x CUSTOM TAXONOMY 2 chi’ is closed to new replies.