[wp-hackers] boolean logic with tag and categorie url params?

Otto otto at ottodestruct.com
Tue Jun 21 21:09:50 UTC 2011

So the query is "(tag1 AND tag2) AND (tag3 OR tag4)", correct?

You won't be able to do something so complex from the URL alone, for
sure. URL parsing is intentionally simple in this respect. No amount
of weird syntax will cover all cases.

But you might get it with an advanced taxonomy query. I have not tried
it, however this might do the trick (and it might only work in 3.2 and
up too):

query_posts(array('tax_query' => array(
	'relation' => 'AND',
		'taxonomy' => 'post_tag',
		'terms' => array('tag1', 'tag2'),
		'field' => 'slug',
		'operator' => 'AND',
		'taxonomy' => 'post_tag',
		'terms' => array('tag3', 'tag4'),
		'field' => 'slug',
		'operator' => 'IN',

To combine that with categories, you'd use the category taxonomy as
another bit in your advanced query.

More info on using advanced queries:


On Tue, Jun 21, 2011 at 3:23 PM, Bjorn Wijers <burobjorn at gmail.com> wrote:
> Hi,
> I've been (re)evaluating WordPress' ability to query post using _only_
> the tag and category parameters for a project of mine. It's been a while
> using this (roughly 3 years according to ticket #5433) and I wondered
> what the current state would be.
> Thus far my conclusion is that it's a lot better then it used to be,
> however there seems no support to be for a combination of boolean AND
> with an boolean OR unless using a combination of categories and tags is
> used. So I cannot force the query to do something like: 'give me all
> posts with tag1 and tag2 and either tag3 or tag4'. Is this correct?
> Here are some examples of the urls I tested (trying to write a coherent
> blog post on this..)
> 1. domain.ext/blogname/?tag=tag1+tag2
> Shows only posts with both tags
> 2. domain.ext/blogname/?tag=tag1+tag2,tag3
> Shows all posts tagged with one or more of these tags tag1, tag2, or
> tag3. It does NOT show the posts with at least tag1 and then either tag2
> or tag3 (or both).
> 3. domain.ext/blogname/?category_name=Category2&tag=tag1+tag2
> Shows only posts in Category2 tagged with both tag1 and tag2
> 4. domain.ext/blogname/?category_name=Category2,Category1&tag=tag1+tag2
> Shows posts in one or both categories as long as the post has been
> tagged with both tags
> Ps: Is there more documentation on the url parameters in relation to
> querying? I tried the Codex, but I might have missed something?
> grtz
> BjornW
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers

More information about the wp-hackers mailing list