Sooner rather than later, you’re going to get asked. No matter how you slice it, people are going to insist they need it. But… how do you go about grouping a list of posts by taxonomy?
I’ll give you an example, referred to in the title of this post: the menu. While menu items are normally listed by a specific menu or category – breakfast, lunch and dinner, for example – many menus require a further subdivision of menu items. Wine lists, for example, need to be broken down by red, white and blush or rose. Beer might get split up between domestic, imported and microbrew. So, if you use a custom taxonomy to organize your menus, you’ll need to be able to further subdivide in this manner.
With my current client, I do a lot of menu work. And yes, you could just list out each menu and submenu with their own loops. But that’s inefficient and just not in the character of a good developer. There has to be a way to make this easier. And there is.
The below code is based on code further derived from a WordPress.org blog post. But while that code worked for a limited set of options, I wanted something more abstracted that could apply to many different types of scenarios. Here it is:
You’ll notice, of course, that this example is highly-specific to menus at the moment. That’s at least in part because I couldn’t get WordPress to load a template part with the right variables. You’ll also note that the call to the second function looks like it’s OOP. That’s because it is. I write my WordPress theme functions.php file as OOP. But otherwise, this should be a very easily-compatible bit of code to fit your own particular needs. Enjoy!