[wp-trac] Re: [WordPress Trac] #5433: Cannot combine category and tag queries in some cases

WordPress Trac wp-trac at lists.automattic.com
Thu Jan 24 18:25:37 GMT 2008


#5433: Cannot combine category and tag queries in some cases
------------------------+---------------------------------------------------
 Reporter:  philhassey  |        Owner:  anonymous
     Type:  defect      |       Status:  new      
 Priority:  high        |    Milestone:  2.6      
Component:  General     |      Version:  2.3.1    
 Severity:  normal      |   Resolution:           
 Keywords:              |  
------------------------+---------------------------------------------------
Changes (by Otto42):

  * priority:  normal => high
  * summary:  URL Queries /?category_name=x&tag=a don't work => Cannot
              combine category and tag queries in some cases

Comment:

 The underlying cause of the problem is how the queries are built.

 The "cat" and "category_name" are mapped to be equivalent to the internal
 "category__in" (or "category_not__in" for negative values of "cat").

 Similarly, "tag" is mapped to be equivalent to the internal "tag_slug__in"
 when you use commas, and "tag_slug__and" when you use plus signs.

 Category__in causes this inner join:
 INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID =
 $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON
 ($wpdb->term_relationships.term_taxonomy_id =
 $wpdb->term_taxonomy.term_taxonomy_id)

 And this to be added to the WHERE clause:
 AND $wpdb->term_taxonomy.taxonomy = 'category'

 Similarly, tag_slug__in (and tag__in) causes the exact same inner join,
 with this added to the WHERE:
 AND $wpdb->term_taxonomy.taxonomy = 'post_tag'

 These two are mutually exclusive, no post can satisfy them. Thus the 404
 error you get. The fix for this is to move the check for 'category' and
 'post_tag' taxonomies into the second inner join, like so:
 INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID =
 $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON
 ($wpdb->term_relationships.term_taxonomy_id =
 $wpdb->term_taxonomy.term_taxonomy_id AND $wpdb->term_taxonomy.taxonomy =
 'category')

 Same for the tags. This causes the join to only join the category and/or
 post tags correctly. There may need to be some "AS tag" and "AS category"
 bits added to make the tags/categories more specific here, in case both
 are included.

 Regarding the multi-tag queries, these use a separate query to get a list
 of post-id's which is then checked by this:
 AND $wpdb->posts.ID IN (list of posts)

 Which is why they combine so readily.

-- 
Ticket URL: <http://trac.wordpress.org/ticket/5433#comment:1>
WordPress Trac <http://trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list