I have a CPT that is a member of two separate taxonomies. On the taxonomy archive page for one of the taxonomies, I need to further group and sort on the second taxonomy like so:
mysite.com/tax1/term1/
tax2-term1
-post
-post
-post
[see more]
tax2-term2
-post
-post
-post
[see more]
… etc.
Better Example
To provide a more easily accessible example, consider the following CPT: Toys. And the two taxonomies: Colour and Age Group.
On the taxonomy archive page for mysite.com/colour/red, I need to further group on the second taxonomy, “age group” like so:
Red Toys
Ages: 0 – 12 months
-toy
-toy
-toy
[see more Red toys for ages 0 – 12 mo]
Ages: 12-24 months
-toy
-toy
-toy
[see more Red toys for ages 12 – 24 mo]
etc…
Intercepting query_posts
So, unless I’m mistaken and we’ve suddenly got the ability to sort on taxonomy (grouping would be ideal, but I could work around that), I’m thinking that a custom SQL query with my own tax joins will be the best way to get what I need. Correct me if I’m wrong. Most of the time I want to go the built-in WP way, I just don’t see that working here.
How would you hijack the main query to still use The_Loop(TM) but with custom SQL? Or would you find a way to return an empty query and then just roll your own custom SQL loop?
Right now I’m leaning toward using pre_get_posts
to set up a posts_clauses
replacement of the join, groupby, orderby and maybe limits clauses, or even a wholesale replacement of the entire request using posts_request
.
Is there an easier way?