Ticket #9951 (closed enhancement: duplicate)
Add [taxonomy]__in, [taxonomy]__not_in to wp_query
| Reported by: |
|
Owned by: |
|
|---|---|---|---|
| Priority: | high | Milestone: | |
| Component: | Query | Version: | 2.8 |
| Severity: | normal | Keywords: | needs-patch early |
| Cc: | westi, johnbillion@…, willmot, simon@…, trac@…, beau@… |
Description (last modified by scribu) (diff)
Currently it's not possible to query for multiple custom taxonomy terms using query_posts the same way you can do tag__in, category__in, tag__not_in etc. I propose wp_query is extended to support querying by multiple custom taxonomy terms and also multiple custom taxonomies and terms.
For example, to get all posts that are in my 'Microsoft' term in the 'company' tax:
query_posts( array( 'company__in' => array( 5 ) ) ) //5 is the term_id for Microsoft
Multiple taxs with multiple terms:
query_posts( array(
'company__in' => array( 5, 10 ),
'people__in' => array( 11, 12, 13 ),
));
Basically: [tax]__in, [tax]__not_in and [tax]__slug_in should all work, in conjunction with tag__in etc.
I have written a patch that implements this, currently [tax]__in, [tax]__not_in, but [tax]__slug_in will be an easy addition.
As tag__in, category__in and all of those are no different, the patched lines I wrote could replace all those hardcoded statements fairly easily.
Attachments
Change History
-
attachment
query.diff.txt
added
- Milestone changed from 2.8 to Future Release
Too late for 2.8. Postponing.
- Cc westi added
-1 to current patch.
Much better if this was an explicit action caused by register_taxonomy that searching for xxx in query var names.
Modified the patch to not use strpos() on $qv names, now foreach's through global wp_taxonomies and does an array_key_exists() instead, let me know if I am heading in the wrong direction with this
- Keywords needs-patch added; has-patch removed
- Priority changed from high to normal
- Milestone changed from 2.9 to Future Release
File to patch: wp-includes/query.php patching file wp-includes/query.php Hunk #2 FAILED at 1920. patch: **** malformed patch at line 85: \ No newline at end of file
-
attachment
query.diff
added
Fixed problem with diff, sorry about that - not too used to all this svn crazyness!
comment:11
willmot — 2 years ago
- Cc willmot added
- Keywords tested added
I have tested this against Wordpress 2.8.4 on a large site that is soon to be released.
The $join on line 1925 is not needed (it joins the taxonomy and term tables on when they are not used in the WHERE). Removing it speeds things up considerably, especially on installs which have thousands of terms.
Attached patch simply removes said line.
comment:12
willmot — 2 years ago
- Priority changed from normal to high
Is there anything else that can be done by us to help get this into WordPress 2.9?
We are currently using the patch on two client sites ( http://www.bestbuytables.co.uk is one of them) and it would be better if this were in core so that I don't have to apply the patch again when 2.9 is released.
I think this patch should be high priority as there is currently no way to query using multiple taxonomies in WordPress.
comment:13
ryan — 2 years ago
- Keywords early added
- Milestone changed from 2.9 to Future Release
It's too late for 2.9 due to the freeze. We can do it early in the next release.
By the way, your patch looks like it backs out some recent changes in trunk.
comment:14
willmot — 2 years ago
Shame that we couldn't get it into 2.9 as the original patch and ticket were submitted pre 2.8, ah well...
I will add another patch diff'd properly against trunk asap.
-
attachment
tax-experimental.diff
added
Added experimental patch which replaces all the category*, tag* to use all registered taxonomies. (not working)
comment:15
in reply to:
↑ 3
;
follow-up:
↓ 16
scribu — 2 years ago
- Keywords needs-patch added; has-patch needs-review tested removed
- Owner changed from filosofo to ryan
- Component changed from Taxonomy to Query
- Milestone changed from Future Release to 3.0
Replying to westi:
-1 to current patch.
Much better if this was an explicit action caused by register_taxonomy that searching for xxx in query var names.
Agree: You should just be able to do my_term=foo&my_other_term=bar
comment:16
in reply to:
↑ 15
joehoyle — 2 years ago
Replying to scribu:
Replying to westi:
-1 to current patch.
Much better if this was an explicit action caused by register_taxonomy that searching for xxx in query var names.
Agree: You should just be able to do my_term=foo&my_other_term=bar
Don't quite understand what you mean here. Do you mean add another arg to register_taxonomy() to specify a query var to be used in query_posts()?
comment:17
scribu — 2 years ago
Do you mean add another arg to register_taxonomy() to specify a query var to be used in query_posts()?
The query_var argument is already available: http://core.trac.wordpress.org/browser/tags/2.9.1/wp-includes/taxonomy.php#L167
Say you have a 'genre' custom taxonomy.
If it's hierarchical, these are the query vars that should become available:
'genre', 'genre__in', 'genre__not_in', 'genre__and'
...in other words, the exact same query vars that are currently available for categories.
If it's not hierarchical, these are the query vars that should become available:
'genre', 'genre__in', 'genre__not_in', 'genre__and', 'genre_slug__in', 'genre_slug__and'
...in other words, the exact same query vars that are currently available for tags.
comment:18
nacin — 2 years ago
- Owner changed from ryan to MarkJaquith
- Status changed from new to assigned
- Milestone changed from 3.0 to Future Release
Moving to future for now. I know MarkJaquith was working on a possible new API for this.
comment:19
scribu — 2 years ago
Related: #12413
comment:21
markjaquith — 22 months ago
Related (more ambitious): #12891
These things are not necessarily mutually exclusive. What I'm proposing is more of an advanced API specifically for multi-taxonomy queries. It'd be overkill for some simple OR examples that this ticket could handle.
comment:24
scribu — 19 months ago
Marked #14351 as dup of this.
comment:26
scribu — 18 months ago
Marked #14620 as dup of this.
comment:27
scribu — 17 months ago
- Status changed from assigned to closed
- Resolution set to duplicate
- Milestone Future Release deleted
Marking this as duplicate of #12891 so we have a single place for discussion.

Patch to allow [custom taxonomy]in in wp_query