Is it possible to sort posts (custom post type) by category / taxonomy (name, desc)?
For example my categories would be
- Season 2012
- some post
- some post
- some post
- Season 2011
- some post
- some post
- some post
- Season 2010
- etc..
Is it possible to sort posts (custom post type) by category / taxonomy (name, desc)?
For example my categories would be
You must be logged in to post a comment.
One solution would be-
I know this is an old question that’s already got a great answer, but I struggled with something very similar for hours today and came up with an alternate loop.
You can also customize Mridul’s (excellent) solution a bit if you want to make this work on the archive page for a taxonomy. In other words, you can sort a custom taxonomy archive by another taxonomy using this solution.
I’m sharing this because I spent four hours struggling with this problem today, and if anybody else is trying to create custom taxonomy archives sorted by a different taxonomy, this is how you do it. (Also, this is my first answer on StackExchange รขยย please be nice! ๐ )
This begins exactly like Mridul’s solution. Query the taxonomy you want to sort your posts by.
This is where it gets a bit different from Mridul’s answer. You want to query two taxonomies, and make sure that the relationship between them is set to be “AND”, which means that the listed posts meet the criteria of being in both taxonomies.
In this case, the posts in “custom_post_type” must match the first term’s slug and another’s name. Hopefully the code makes sense to everybody!
Alternative loop, where
tax = category
and custompost type = "results"