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.

Have you looked at tax_query for WP_Query?
–
mor7iferApr 4 '12 at 23:08

+1 for a fun read. @m0r7if3r He wants a one tax query and then sort by the 2nd taxonomy terms.
–
kaiser♦Apr 5 '12 at 0:41

@kaiser Yea, I was just having a hard time understanding what exactly was trying to be done...the way I read it, it was within tax_query's capabilities, and I was hoping he'd report back that that did it, and if not, how that fails to meet his needs. I think your answer got it though.
–
mor7iferApr 5 '12 at 1:14

@m0r7if3r Hopefully I got a little closer. Pretty shure that it's not the actual solution. Just yesterday mod told me that it's a bad habbit to answer Qs like that (guessing and asking). :)
–
kaiser♦Apr 5 '12 at 1:18

1 Answer
1

[A] Multi-Tax queries have the problem that they're doing a JOIN of the meta table for every term. So imho it's not the best idea to use the built in way.

[B.1.a] I'd simply do a normal query with a one (main-)tax arg and then do the sorting depending on the meta values. So actually just calling get_post_custom() and merging it with a single post object during a loop and then appending it to a resulting array.

[B.1.b] Then modifying the query through posts_clauses if some bits doesn't meet the requirements.

[B.2] Start sorting for e.g. by name.

[B.3] Then I'd go and run some foreach/etc loop on the resulting array.

The benefit of browser/php vs. DB isn't there so far (as long as the site hasn't got much hits). But if you start minifying css/js/html and caching the resulting stuff (and delivering the results from the hard disk), I think this would be the way I'd go.