4

I've been banging my head about this, and have created a work-around, but it would be much nicer for my site's organisation if I could figure this out.

I want to be able to query posts across two taxonomies (one of which is custom), I'd like to be able to do this automagically, without needing to create custom templates with custom DB queries.

I want to have a platform custom taxonomy, and in the same time to be able to label articles as category (such as review or preview terms).

At the moment I use a customised Walker to allow me to use the built-in Menu system to generate menus that point to specific archive pages.

My URL looks like:

example.com/category/platforms/consoles-platforms/ps3+review/

This finds articles that are in the PS3 portion of platforms AND in the Review category. Currently this is all done under the normal wordpress category taxonomy.

Ideally I'd want them to be separate taxonomies, (Reviews being part of normal Category tax and then a Platform tax for console organisation) so that the URL could look like:

example.com/platforms/consoles/ps3/review

It looks nicer and would let me keep my categories more tightly organised. BUT I can't for the life of me see how you set up WordPress to query across two taxonomies automatically.

Has anyone else come up against this?

Edit: Following Rarst's comment, I have to wonder, am I being too ambitious with my URL requirement? I could feasibly set this up using GET parameters and just one template (i.e., example.com?platform=ps3&type=review); is that the simplest/most robust way? Any other ideas on how to solve this?

4
  • I think you might want to rephrase title and question a bit. Querying itself is trivial or easy (mostly... :) but what you want is complex URL structure, which is entirely different story.
    – Rarst
    Commented Jan 11, 2012 at 11:11
  • I've edited the question some, and realise I could do it with GET variable, although I would rather a nice URL structure if possible. Or rather: if there is a simple Wordpress way to query across taxonomies, that would be a good thing to know. Thanks Rarst!
    – josh
    Commented Jan 11, 2012 at 17:29
  • 1
    There are probably ways to do this with the wordpress rewrite engine, but I'm not an expert in that. I know buddypress accomplishes something similar, however it's not simple. Try downloading buddypress then look in buddypress/bp-core/bp-core-catchuri.php, you will see how they explode the url into it's component parts, then from there you can find out what should be loaded and hook into the wp template loader.
    – dwenaus
    Commented Jan 12, 2012 at 7:31
  • The answer to this would involve a custom rewrite rule
    – Tom J Nowell
    Commented May 3, 2014 at 20:41

1 Answer 1

1

I think you could get it working with category__and

I remember trying a similar approach to yours and found out that I would get content that was either in platform and/or in review. But to get actually posts that where only on both, excluding posts that were just in one of them, this was my approach. Maybe the way you have your posts works with that approach, but it didn't with mine (which was more like city / type of event / cost of the event).

Anyway, you could pass your combination of elements through POST, and then on the receiving page get those elements into the array:

$myFilteredArray = array();
if (isset($_POST['.... 
// Do what you need to get the POST variable elements
into the array

// and then, use this query format
query_posts( array(
            'category__and' => $myFilteredArray, 
            'orderby' => 'title',
            'posts_per_page'=>-1, 
            'order' => 'ASC')
            );   
4
  • 1
    I would strongly advise against the use of query_posts
    – Tom J Nowell
    Commented May 3, 2014 at 20:39
  • Would you elaborate on why? Commented May 3, 2014 at 22:32
  • 1
    Taken from the codex from the query_posts page "Note: This function isn't meant to be used by plugins or themes. As explained later, there are better, more performant options to alter the main query" Commented May 4, 2014 at 6:40
  • 1
    query posts works by taking the database queries WP has made, and throwing them away and starting over from scratch. This is incredibly innefficient and slow. Instead use the pre_get_posts filter to modify the original query rather than create a new one. For all other cases use WP_Query or get_posts. Never use query_posts itself
    – Tom J Nowell
    Commented May 6, 2014 at 12:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.