Ticket #5433: 5433v2.diff

File 5433v2.diff, 3.4 KB (added by turboguy, 4 years ago)

Does not use distinct

  • query.php

     
    10941094                                        $tag = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db'); 
    10951095                                        $q['tag_slug__in'][] = $tag; 
    10961096                                } 
    1097                         } else if ( preg_match('/[+\s]+/', $q['tag']) ) { 
     1097                        } else if ( preg_match('/[+\s]+/', $q['tag']) || !empty($q['cat']) ) { 
    10981098                                $tags = preg_split('/[+\s]+/', $q['tag']); 
    10991099                                foreach ( (array) $tags as $tag ) { 
    11001100                                        $tag = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db'); 
     
    11111111                        $groupby = "{$wpdb->posts}.ID"; 
    11121112                } 
    11131113 
    1114                 if ( !empty($q['tag__in']) ) { 
     1114                if ( !empty($q['tag__in']) && empty($q['cat']) ) { 
    11151115                        $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) "; 
    11161116                        $whichcat .= " AND $wpdb->term_taxonomy.taxonomy = 'post_tag' "; 
    11171117                        $include_tags = "'" . implode("', '", $q['tag__in']) . "'"; 
     
    11211121                                $q['tag_id'] = $reqtag['term_id']; 
    11221122                } 
    11231123 
    1124                 if ( !empty($q['tag_slug__in']) ) { 
     1124                if ( !empty($q['tag_slug__in']) && empty($q['cat']) ) { 
    11251125                        $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) "; 
    11261126                        $whichcat .= " AND $wpdb->term_taxonomy.taxonomy = 'post_tag' "; 
    11271127                        $include_tags = "'" . implode("', '", $q['tag_slug__in']) . "'"; 
     
    11401140                } 
    11411141 
    11421142                // Tag and slug intersections. 
    1143                 $intersections = array('category__and' => 'category', 'tag__and' => 'post_tag', 'tag_slug__and' => 'post_tag'); 
     1143                $intersections = array('category__and' => 'category', 'tag__and' => 'post_tag', 'tag_slug__and' => 'post_tag', 'tag__in' => 'post_tag', 'tag_slug__in' => 'post_tag'); 
     1144                $tagin = array('tag__in', 'tag_slug__in'); // These are used to make some exceptions below 
    11441145                foreach ($intersections as $item => $taxonomy) { 
    11451146                        if ( empty($q[$item]) ) continue; 
    1146  
     1147                        if ( in_array($item, $tagin) && empty($q['cat']) ) continue; // We should already have what we need if categories aren't being used 
     1148                         
    11471149                        if ( $item != 'category__and' ) { 
    11481150                                $reqtag = is_term( $q[$item][0], 'post_tag' ); 
    11491151                                if ( !empty($reqtag) ) 
    11501152                                        $q['tag_id'] = $reqtag['term_id']; 
    11511153                        } 
    11521154 
    1153                         $taxonomy_field = $item == 'tag_slug__and' ? 'slug' : 'term_id'; 
     1155                        $taxonomy_field = $item == ('tag_slug__and' || 'tag_slug__in') ? 'slug' : 'term_id'; 
    11541156 
    11551157                        $q[$item] = array_unique($q[$item]); 
    11561158                        $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)"; 
    11571159                        $tsql .= " WHERE tt.taxonomy = '$taxonomy' AND t.$taxonomy_field IN ('" . implode("', '", $q[$item]) . "')"; 
    1158                         $tsql .= " GROUP BY p.ID HAVING count(p.ID) = " . count($q[$item]); 
    1159  
     1160                        if ( !in_array($item, $tagin) ) { // This next line is only helpful if we are doing an and relationship 
     1161                                $tsql .= " GROUP BY p.ID HAVING count(p.ID) = " . count($q[$item]); 
     1162                        } 
    11601163                        $post_ids = $wpdb->get_col($tsql); 
    11611164 
    11621165                        if ( count($post_ids) )