diff --git a/wp-includes/query.php b/wp-includes/query.php index b9c4d29635..e7b89ffc49 100644 --- a/wp-includes/query.php +++ b/wp-includes/query.php @@ -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)"; + } } } diff --git a/wp-includes/wp-db.php b/wp-includes/wp-db.php index b0b37e4960..7b6c6f090f 100644 --- a/wp-includes/wp-db.php +++ b/wp-includes/wp-db.php @@ -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. *