Displaying Custom Post Types by Term

It’s documented, it’s used, but it wasn’t obvious to me. After hunting around, I finally found what I needed in the first place I looked: the WordPress Codex.

This is the wp_query argument needed to display posts assigned a particular term:

Using the term ID as a cat ID wasn’t working. I came across the above here, and used it in two simple loops to display the custom posts under category headers.

Want to display posts in a nested hierarchy? This goal led me to what is probably a cleaner solution. The following code is adapted from Hierarchical Category List with Post Titles, and blended with the code above.

It’s in action here under Sites

48 thoughts on “Displaying Custom Post Types by Term”

  1. Hi Daniel,

    I’ve been searching for a solution like this, however, I am getting PHP errors at line 23 due to the extra ‘}’, and, in my functions.php due to something else that I haven’t been able to identify. I’d love to implement this snippet, could you check that everything is as it should be?

    Many thanks,

  2. Hi everyone,

    I’ve altered the code above, adding the first line now shown. I think the issue reported is simply that the first function isn’t declared as a function.

    This isn’t a complete solution. You’ll need to call the first function, like in a shortcode or other hook.

Leave a Reply