Make WordPress Core

Ticket #5137: intersection.2.diff

File intersection.2.diff, 3.0 KB (added by xknown, 18 years ago)

New query for tag and category intersections

  • wp-includes/query.php

     
    948948                        }
    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'] ) {
    962953                        $reqcat = get_category_by_path($q['category_name']);
     
    10511042                        }
    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++;
     1045                $intersections = array('category__and' => 'category', 'tag__and' => 'post_tag', 'tag_slug__and' => 'post_tag');
     1046                foreach ($intersections as $item => $taxonomy) {
     1047                        if ( empty($q[$item]) ) continue;
     1048
     1049                        if ( $item != 'category__and' ) {
     1050                                $reqtag = is_term( $q[$item][0], 'post_tag' );
     1051                                if ( !empty($reqtag) )
     1052                                        $q['tag_id'] = $reqtag['term_id'];
    10601053                        }
    1061                         $reqtag = is_term( $q['tag__and'][0], 'post_tag' );
    1062                         if ( !empty($reqtag) )
    1063                                 $q['tag_id'] = $reqtag['term_id'];
    1064                 }
    10651054
    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++;
     1055                        $taxonomy_field = $item == 'tag_slug__and' ? 'slug' : 'term_id';
     1056
     1057                        $q[$item] = array_unique($q[$item]);
     1058                        $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)";
     1059                        $tsql .= "WHERE tt.taxonomy = '$taxonomy' AND t.$taxonomy_field IN ('" . implode("', '", $q[$item]) . "')";
     1060                        $tsql .= "GROUP BY p.ID HAVING count(p.ID) = " . count($q[$item]);
     1061
     1062                        $post_ids = $wpdb->get_col($tsql);
     1063
     1064                        if ( count($post_ids) )
     1065                                $where .= "AND $wpdb->posts.ID IN (" . implode(', ', $post_ids) . ") ";
     1066                        else {
     1067                                $where = "AND 0 = 1";
     1068                                break;
    10721069                        }
    1073                         $reqtag = is_term( $q['tag_slug__and'][0], 'post_tag' );
    1074                         if ( !empty($reqtag) )
    1075                                 $q['tag_id'] = $reqtag['term_id'];
    10761070                }
    10771071
    10781072                // Author/user stuff