From 87bb8cd69cc593fe6bed330fb1791eac9df87167 Mon Sep 17 00:00:00 2001 From: donncha Date: Tue, 23 Oct 2007 18:28:40 +0000 Subject: Merge with WordPress, rev 6285 and untested git-svn-id: http://svn.automattic.com/wordpress-mu/trunk@1125 7be80a69-a1ef-0310-a953-fb0f7c49ff36 --- wp-includes/query.php | 68 +++++++++++++++++++++------------------------------ 1 file changed, 28 insertions(+), 40 deletions(-) (limited to 'wp-includes/query.php') diff --git a/wp-includes/query.php b/wp-includes/query.php index 49cff8b..fad901a 100644 --- a/wp-includes/query.php +++ b/wp-includes/query.php @@ -932,7 +932,7 @@ class WP_Query { } if ( !empty($q['category__in']) ) { - $join = " LEFT JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) LEFT JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id) "; + $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) "; $whichcat .= " AND $wpdb->term_taxonomy.taxonomy = 'category' "; $include_cats = "'" . implode("', '", $q['category__in']) . "'"; $whichcat .= " AND $wpdb->term_taxonomy.term_id IN ($include_cats) "; @@ -948,15 +948,6 @@ class WP_Query { } } - if ( !empty($q['category__and']) ) { - $count = 0; - foreach ( $q['category__and'] as $category_and ) { - $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) "; - $whichcat .= " AND tt$count.term_id = '$category_and' "; - $count++; - } - } - // Category stuff for nice URLs if ( '' != $q['category_name'] ) { $reqcat = get_category_by_path($q['category_name']); @@ -982,7 +973,7 @@ class WP_Query { $q['cat'] = $reqcat; - $join = " LEFT JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) LEFT JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id) "; + $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) "; $whichcat = " AND $wpdb->term_taxonomy.taxonomy = 'category' "; $in_cats = array($q['cat']); $in_cats = array_merge($in_cats, get_term_children($q['cat'], 'category')); @@ -1007,14 +998,7 @@ class WP_Query { } } else { $q['tag'] = sanitize_term_field('slug', $q['tag'], 0, 'post_tag', 'db'); - $reqtag = is_term( $q['tag'], 'post_tag' ); - if ( !empty($reqtag) ) - $reqtag = $reqtag['term_id']; - else - $reqtag = 0; - - $q['tag_id'] = $reqtag; - $q['tag__in'][] = $reqtag; + $q['tag_slug__in'][] = $q['tag']; } } @@ -1024,7 +1008,7 @@ class WP_Query { } if ( !empty($q['tag__in']) ) { - $join = " LEFT JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) LEFT JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id) "; + $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) "; $whichcat .= " AND $wpdb->term_taxonomy.taxonomy = 'post_tag' "; $include_tags = "'" . implode("', '", $q['tag__in']) . "'"; $whichcat .= " AND $wpdb->term_taxonomy.term_id IN ($include_tags) "; @@ -1034,7 +1018,7 @@ class WP_Query { } if ( !empty($q['tag_slug__in']) ) { - $join = " LEFT JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) LEFT JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id) LEFT JOIN $wpdb->terms ON ($wpdb->term_taxonomy.term_id = $wpdb->terms.term_id) "; + $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) "; $whichcat .= " AND $wpdb->term_taxonomy.taxonomy = 'post_tag' "; $include_tags = "'" . implode("', '", $q['tag_slug__in']) . "'"; $whichcat .= " AND $wpdb->terms.slug IN ($include_tags) "; @@ -1051,28 +1035,32 @@ class WP_Query { } } - if ( !empty($q['tag__and']) ) { - $count = 0; - foreach ( $q['tag__and'] as $tag_and ) { - $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) "; - $whichcat .= " AND tt$count.term_id = '$tag_and' "; - $count++; + // Tag and slug intersections. + $intersections = array('category__and' => 'category', 'tag__and' => 'post_tag', 'tag_slug__and' => 'post_tag'); + foreach ($intersections as $item => $taxonomy) { + if ( empty($q[$item]) ) continue; + + if ( $item != 'category__and' ) { + $reqtag = is_term( $q[$item][0], 'post_tag' ); + if ( !empty($reqtag) ) + $q['tag_id'] = $reqtag['term_id']; } - $reqtag = is_term( $q['tag__and'][0], 'post_tag' ); - if ( !empty($reqtag) ) - $q['tag_id'] = $reqtag['term_id']; - } - if ( !empty($q['tag_slug__and']) ) { - $count = 0; - foreach ( $q['tag_slug__and'] as $tag_and ) { - $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) "; - $whichcat .= " AND term$count.slug = '$tag_and' "; - $count++; + $taxonomy_field = $item == 'tag_slug__and' ? 'slug' : 'term_id'; + + $q[$item] = array_unique($q[$item]); + $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)"; + $tsql .= " WHERE tt.taxonomy = '$taxonomy' AND t.$taxonomy_field IN ('" . implode("', '", $q[$item]) . "')"; + $tsql .= " GROUP BY p.ID HAVING count(p.ID) = " . count($q[$item]); + + $post_ids = $wpdb->get_col($tsql); + + if ( count($post_ids) ) + $whichcat .= " AND $wpdb->posts.ID IN (" . implode(', ', $post_ids) . ") "; + else { + $whichcat = " AND 0 = 1"; + break; } - $reqtag = is_term( $q['tag_slug__and'][0], 'post_tag' ); - if ( !empty($reqtag) ) - $q['tag_id'] = $reqtag['term_id']; } // Author/user stuff -- cgit