From 13cf1a5c78aa2a2631a03b1e903f9d902d18ea09 Mon Sep 17 00:00:00 2001 From: Ryan Boren Date: Tue, 26 Aug 2008 19:18:58 +0000 Subject: [PATCH] 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 --- wp-includes/query.php | 17 +++++++++++------ wp-includes/wp-db.php | 11 +++++++++++ 2 files changed, 22 insertions(+), 6 deletions(-) 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. *