Ticket #9951 (closed enhancement: duplicate)

Opened 3 years ago

Last modified 17 months ago

Add [taxonomy]__in, [taxonomy]__not_in to wp_query

Reported by: joehoyle Owned by: MarkJaquith
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

query.diff.txt Download (2.5 KB) - added by joehoyle 3 years ago.
Patch to allow [custom taxonomy]in in wp_query
query.diff Download (4.1 KB) - added by joehoyle 3 years ago.
Fixed problem with diff, sorry about that - not too used to all this svn crazyness!
query.2.diff Download (8.6 KB) - added by willmot 2 years ago.
Removed unneeded $join
tax-experimental.diff Download (8.4 KB) - added by joehoyle 2 years ago.
Added experimental patch which replaces all the category*, tag* to use all registered taxonomies. (not working)

Change History

Patch to allow [custom taxonomy]in in wp_query

comment:1   ryan3 years ago

  • Milestone changed from 2.8 to Future Release

Too late for 2.8. Postponing.

  • Keywords has-patch added

comment:3 follow-up: ↓ 15   westi3 years ago

  • 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

  • Milestone changed from Future Release to 2.9
  • 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

Fixed problem with diff, sorry about that - not too used to all this svn crazyness!

  • Keywords has-patch added; needs-patch removed
  • Keywords needs-review added
  • Milestone changed from Future Release to 2.9
  • Cc johnbillion@… added

willmot2 years ago

Removed unneeded $join

  • 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.

  • 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.

  • 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.

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.

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   scribu2 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   joehoyle2 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()?

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.

  • 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.

Related: #12413

  • Cc simon@… added

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.

  • Cc trac@… added
  • Cc beau@… added

Marked #14351 as dup of this.

  • Description modified (diff)

Marked #14620 as dup of this.

  • 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.

Note: See TracTickets for help on using tickets.