• Hi everyone,

    I’ve created a hierarchical taxonomy (Recipes) that has two main subgroups (Sweet and Savory) with several values in each subgroup (e.g. Cookies & Cakes in the first one, Appetizers & Entrees in the second one). What I’d like to do is display posts according to something like this:

    GROUP A NAME (SWEET)

    SUBGROUP A.1 NAME (COOKIES)

    [list of posts with this taxonomy value]

    SUBGROUP A.2 NAME (CAKES)

    [list of posts with this taxonomy value]

    GROUP B NAME (SAVORY)

    SUBGROUP B.1 NAME (APPETIZERS)

    [list of posts with this taxonomy value]

    SUBGROUP B.2 NAME (ENTREES)

    [list of posts with this taxonomy value]

    Ideally I’d like new subgroups to appear dynamically as I continue to create more of them in the admin panel. Is this feasible?
    My site is https://www.alittlebiteofeverything.com and I’m using a child theme of twenty-twelve.

    Any pointers on how to do this would be appreciated. I know I should create a page template but I’m not sure where to go from there. The only page template I’ve created so far by myself is the tag.php (you can view it in action here https://www.alittlebiteofeverything.com/tag/chocolate/

    Thanks!

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

    (@bcworkz)

    If I may question your layout before I answer. The layout looks great for a few dozen recipes. What about when it grows to several hundred? Nobody wants to scroll through hundreds of cookie recipes to get to savory entrees. Will it be broken up into more pages at some point, or will you limit the number by some metric; ratings, newest, quickest, etc. I don’t really care, but the answer may influence how you proceed now.

    You have two basic choices. A grand all encompassing query with proper orderby and groupby arguments that returns everything properly structured, and your template code sorts each group into the proper divisions. Or you could do a more targeted query for one division, then a new query for each next division. Or some variation in between.

    It’s generally more efficient to limit database queries, but can make coding more complex. Multiple targeted queries would make things much easier to break up in the future. Either concept can adapt to added terms in the future, as terms are just more items in the same loop. It’s as easy to do one as one hundred.

    If your structure is 2 or 3 some levels deep, you may want to commit to that, but if there’s lots of levels, you’ll want to look at reentrant loops, which are tricky to code, but ultimately flexible. WP has a walker class which makes this easier though.

    Thread Starter ainhoa711

    (@ainhoa711)

    When I grow to have more recipes I’ll show the latest ones or create index pages (so only show the latest of each category and then link to more). I don’t expect to have more than two levels deep and certainly not more than three.
    I’ve already managed to create a taxonomy.php that sort of works, but not quite. I have another taxonomy (wedding) with five terms and you can see the results here: https://www.alittlebiteofeverything.com/our-wedding/planning/

    The problem is that if I apply the same taxonomy.php to recipes it doesn’t distinguish the upper level terms (savory/sweet) from the lower level ones (cookies/cakes/appetizers/entrees) and treats them all equally, showing results for all of them.

    So basically I already managed to write a taxonomy.php but I could use some help modifying the code so that it only shows results of “children” terms and not of their parents’. Hope that makes sense, and thanks for your help!

    Moderator bcworkz

    (@bcworkz)

    That looks like a good start! If you paste a copy of your taxonomy.php into pastebin.com or similar, and link to it here, hopefully someone will be able to offer useful suggestions. It’s sometimes hard to deduce what’s going on from just looking at the output.

    Thread Starter ainhoa711

    (@ainhoa711)

    Done: https://pastebin.com/mKDvM6Zc
    Please be kind with my code, I have no idea what I’m doing… all of it was trial & error.

    Moderator bcworkz

    (@bcworkz)

    You seem to have developed some idea what you’re doing. Trial & error for learning coding works well for me, apparently as for you.

    Not really my business (meaning you don’t have to do this), but you might consider adding another level below cookies, entrees, etc. You don’t have to make use of it now, but it’ll be easier to implement in the future if provisions are put in place initially.

    I think it’ll be best to go with the targeted query for each category as a strategy. Things are even easier since posts are output for terms on one level, if I understand correctly. I believe all recipes are output for the cookies etc. level, no recipes exist at the sweet level that have no child terms that would need to be output directly under Sweet.

    You’ll want to make use of the $args parameter when you use get_terms(). It’s the same array format as with WP_Query, except the keys are different. You will have nested foreach loops for each term level. You will do the WP_Query very much as you are now, except it’ll be limited to a particular term on that level. You’ll want to use the tax_query format in WP_Query $args because you don’t want to include children, which would be that extra level we are not yet using.

    Anyway, back to get_terms() arguments. You will want to limit the returned terms to immediate children and a particular parent. So start by getting the top level terms for the recipe taxonomy, which will be sweet and savory.

    Foreach top level term, get the immediate child terms. Also output whatever title or header you want for the top level Sweet, Savory terms.

    Nested inside the top level foreach, you will do another get_terms() and foreach for the immediate children. Cookies, Cakes, etc. Output the sub title or sub header. Feed the term to the WP_Query $args and output the returned posts.

    It will be easy to slip in an extra get_terms() level so that posts are output separately for each type of cookie, cake, etc. in the future.

    In summary, you code will be structured like this pseudocode example:

    get_terms('recipes', array('parent'=>0))
    foreach term {
      echo $term
      get_subterms('recipes', array('parent'=>$term))
      foreach subterm {
        echo $subterm
        WP_Query(tax_query=>array('terms'=>$subterm,'children_of'=>false))
        foreach post {
          $post->the_post()
        }
      }
    }

    I hope you’re getting some enjoyment out of this. Cheers.

    Thread Starter ainhoa711

    (@ainhoa711)

    Thanks! You’re right, everything will be in the cookies level (at least that’s the idea). I’ll create a third level and start working on the code… thanks for your help!!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Display posts grouped by taxonomy’ is closed to new replies.