Changeset 6186

Show
Ignore:
Timestamp:
10/04/07 18:10:03 (9 months ago)
Author:
ryan
Message:

New taxonomy intersection queries from xknown. fixes #5137

Files:

Legend:

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

    r6125 r6186  
    949949        } 
    950950 
    951         if ( !empty($q['category__and']) ) { 
    952             $count = 0; 
    953             foreach ( $q['category__and'] as $category_and ) { 
    954                 $join .= " LEFT JOIN $wpdb->term_relationships AS tr$count ON ($wpdb->posts.ID = tr$count.object_id) LEFT JOIN $wpdb->term_taxonomy AS tt$count ON (tr$count.term_taxonomy_id = tt$count.term_taxonomy_id) "; 
    955                 $whichcat .= " AND tt$count.term_id = '$category_and' "; 
    956                 $count++; 
    957             } 
    958         } 
    959  
    960951        // Category stuff for nice URLs 
    961952        if ( '' != $q['category_name'] ) { 
     
    10521043        } 
    10531044 
    1054         if ( !empty($q['tag__and']) ) { 
    1055             $count = 0; 
    1056             foreach ( $q['tag__and'] as $tag_and ) { 
    1057                 $join .= " LEFT JOIN $wpdb->term_relationships AS tr$count ON ($wpdb->posts.ID = tr$count.object_id) LEFT JOIN $wpdb->term_taxonomy AS tt$count ON (tr$count.term_taxonomy_id = tt$count.term_taxonomy_id) "; 
    1058                 $whichcat .= " AND tt$count.term_id = '$tag_and' "; 
    1059                 $count++; 
    1060             } 
    1061             $reqtag = is_term( $q['tag__and'][0], 'post_tag' ); 
    1062             if ( !empty($reqtag) ) 
    1063                 $q['tag_id'] = $reqtag['term_id']; 
    1064         } 
    1065  
    1066         if ( !empty($q['tag_slug__and']) ) { 
    1067             $count = 0; 
    1068             foreach ( $q['tag_slug__and'] as $tag_and ) { 
    1069                 $join .= " LEFT JOIN $wpdb->term_relationships AS tr$count ON ($wpdb->posts.ID = tr$count.object_id) LEFT JOIN $wpdb->term_taxonomy AS tt$count ON (tr$count.term_taxonomy_id = tt$count.term_taxonomy_id) LEFT JOIN $wpdb->terms AS term$count ON (tt$count.term_id = term$count.term_id) "; 
    1070                 $whichcat .= " AND term$count.slug = '$tag_and' "; 
    1071                 $count++; 
    1072             } 
    1073             $reqtag = is_term( $q['tag_slug__and'][0], 'post_tag' ); 
    1074             if ( !empty($reqtag) ) 
    1075                 $q['tag_id'] = $reqtag['term_id']; 
     1045        // Tag and slug intersections. 
     1046        $intersections = array('category__and' => 'category', 'tag__and' => 'post_tag', 'tag_slug__and' => 'post_tag'); 
     1047        foreach ($intersections as $item => $taxonomy) { 
     1048            if ( empty($q[$item]) ) continue; 
     1049 
     1050            if ( $item != 'category__and' ) { 
     1051                $reqtag = is_term( $q[$item][0], 'post_tag' ); 
     1052                if ( !empty($reqtag) ) 
     1053                    $q['tag_id'] = $reqtag['term_id']; 
     1054            } 
     1055 
     1056            $taxonomy_field = $item == 'tag_slug__and' ? 'slug' : 'term_id'; 
     1057 
     1058            $q[$item] = array_unique($q[$item]); 
     1059            $tsql = "SELECT p.ID FROM $wpdb->posts p LEFT JOIN $wpdb->term_relationships tr ON (p.ID = tr.object_id) LEFT JOIN $wpdb->term_taxonomy tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id) LEFT JOIN $wpdb->terms t ON (tt.term_id = t.term_id)"; 
     1060            $tsql .= "WHERE tt.taxonomy = '$taxonomy' AND t.$taxonomy_field IN ('" . implode("', '", $q[$item]) . "')"; 
     1061            $tsql .= "GROUP BY p.ID HAVING count(p.ID) = " . count($q[$item]); 
     1062 
     1063            $post_ids = $wpdb->get_col($tsql); 
     1064 
     1065            if ( count($post_ids) ) 
     1066                $whichcat .= " AND $wpdb->posts.ID IN (" . implode(', ', $post_ids) . ") "; 
     1067            else { 
     1068                $whichcat = " AND 0 = 1"; 
     1069                break; 
     1070            } 
    10761071        } 
    10771072