Make WordPress Core


Ignore:
Timestamp:
05/15/2009 11:47:28 PM (15 years ago)
Author:
ryan
Message:

Allow combining category and tag queries. Props turboguy. fixes #5433

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/query.php

    r11323 r11348  
    18561856                    $q['tag_slug__in'][] = $tag;
    18571857                }
    1858             } else if ( preg_match('/[+\s]+/', $q['tag']) ) {
     1858            } else if ( preg_match('/[+\s]+/', $q['tag']) || !empty($q['cat']) ) {
    18591859                $tags = preg_split('/[+\s]+/', $q['tag']);
    18601860                foreach ( (array) $tags as $tag ) {
     
    18721872        }
    18731873
    1874         if ( !empty($q['tag__in']) ) {
     1874        if ( !empty($q['tag__in']) && empty($q['cat']) ) {
    18751875            $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) ";
    18761876            $whichcat .= " AND $wpdb->term_taxonomy.taxonomy = 'post_tag' ";
     
    18821882        }
    18831883
    1884         if ( !empty($q['tag_slug__in']) ) {
     1884        if ( !empty($q['tag_slug__in']) && empty($q['cat']) ) {
    18851885            $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) INNER JOIN $wpdb->terms ON ($wpdb->term_taxonomy.term_id = $wpdb->terms.term_id) ";
    18861886            $whichcat .= " AND $wpdb->term_taxonomy.taxonomy = 'post_tag' ";
     
    19041904
    19051905        // Tag and slug intersections.
    1906         $intersections = array('category__and' => 'category', 'tag__and' => 'post_tag', 'tag_slug__and' => 'post_tag');
     1906        $intersections = array('category__and' => 'category', 'tag__and' => 'post_tag', 'tag_slug__and' => 'post_tag', 'tag__in' => 'post_tag', 'tag_slug__in' => 'post_tag');
     1907        $tagin = array('tag__in', 'tag_slug__in'); // These are used to make some exceptions below
    19071908        foreach ($intersections as $item => $taxonomy) {
    19081909            if ( empty($q[$item]) ) continue;
    1909 
     1910            if ( in_array($item, $tagin) && empty($q['cat']) ) continue; // We should already have what we need if categories aren't being used
     1911           
    19101912            if ( $item != 'category__and' ) {
    19111913                $reqtag = is_term( $q[$item][0], 'post_tag' );
     
    19141916            }
    19151917
    1916             $taxonomy_field = $item == 'tag_slug__and' ? 'slug' : 'term_id';
     1918            $taxonomy_field = $item == ('tag_slug__and' || 'tag_slug__in') ? 'slug' : 'term_id';
    19171919
    19181920            $q[$item] = array_unique($q[$item]);
    19191921            $tsql = "SELECT p.ID FROM $wpdb->posts p INNER JOIN $wpdb->term_relationships tr ON (p.ID = tr.object_id) INNER JOIN $wpdb->term_taxonomy tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id) INNER JOIN $wpdb->terms t ON (tt.term_id = t.term_id)";
    19201922            $tsql .= " WHERE tt.taxonomy = '$taxonomy' AND t.$taxonomy_field IN ('" . implode("', '", $q[$item]) . "')";
    1921             $tsql .= " GROUP BY p.ID HAVING count(p.ID) = " . count($q[$item]);
    1922 
     1923            if ( !in_array($item, $tagin) ) { // This next line is only helpful if we are doing an and relationship
     1924                $tsql .= " GROUP BY p.ID HAVING count(p.ID) = " . count($q[$item]);
     1925            }
    19231926            $post_ids = $wpdb->get_col($tsql);
    19241927
Note: See TracChangeset for help on using the changeset viewer.