Use a subquery in category__not_in query if the DB version supports it. Props pedrop. fixes #7599

git-svn-id: https://develop.svn.wordpress.org/trunk@8738 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Ryan Boren 2008-08-26 19:18:58 +00:00
parent f47f0a2ddd
commit 13cf1a5c78
2 changed files with 22 additions and 6 deletions

View File

@ -1046,12 +1046,17 @@ class WP_Query {
}
if ( !empty($q['category__not_in']) ) {
$ids = get_objects_in_term($q['category__not_in'], 'category');
if ( is_wp_error( $ids ) )
return $ids;
if ( is_array($ids) && count($ids > 0) ) {
$out_posts = "'" . implode("', '", $ids) . "'";
$whichcat .= " AND $wpdb->posts.ID NOT IN ($out_posts)";
if ( $wpdb->supports_subqueries() ) {
$cat_string = "'" . implode("', '", $q['category__not_in']) . "'";
$whichcat .= " AND $wpdb->posts.ID NOT IN (SELECT $wpdb->term_relationships.object_id FROM $wpdb->term_relationships WHERE $wpdb->term_relationships.term_taxonomy_id IN ($cat_string) )";
} else {
$ids = get_objects_in_term($q['category__not_in'], 'category');
if ( is_wp_error( $ids ) )
return $ids;
if ( is_array($ids) && count($ids > 0) ) {
$out_posts = "'" . implode("', '", $ids) . "'";
$whichcat .= " AND $wpdb->posts.ID NOT IN ($out_posts)";
}
}
}

View File

@ -923,6 +923,17 @@ class wpdb {
return ( version_compare(mysql_get_server_info($this->dbh), '4.1.0', '>=') );
}
/**
* Whether of not the database version supports sub-queries.
*
* @since 2.7
*
* @return bool True if sub-queries are supported, false if version does not
*/
function supports_subqueries() {
return ( version_compare(mysql_get_server_info($this->dbh), '4.1.0', '>=') );
}
/**
* Retrieve the name of the function that called wpdb.
*