From 83738d47f4220b48e8de9268ff71980b53e444db Mon Sep 17 00:00:00 2001 From: scribu Date: Tue, 9 Nov 2010 23:30:35 +0000 Subject: [PATCH] Make get_tax_sql() a standalone function. See #15032 git-svn-id: https://develop.svn.wordpress.org/trunk@16267 602fd350-edb4-49c9-b593-d223f7449a82 --- wp-includes/class-wp-object-query.php | 55 --------------------------- wp-includes/query.php | 2 +- wp-includes/taxonomy.php | 55 +++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 56 deletions(-) diff --git a/wp-includes/class-wp-object-query.php b/wp-includes/class-wp-object-query.php index 25691445f2..5d4dd025a5 100644 --- a/wp-includes/class-wp-object-query.php +++ b/wp-includes/class-wp-object-query.php @@ -71,61 +71,6 @@ class WP_Object_Query { $qv['meta_query'] = $meta_query; } - /* - * Used internally to generate an SQL string for searching across multiple taxonomies - * - * @access protected - * @since 3.1.0 - * - * @param array $tax_query List of taxonomy queries. A single taxonomy query is an associative array: - * - 'taxonomy' string|array The taxonomy being queried - * - 'terms' string|array The list of terms - * - 'field' string (optional) Which term field is being used. - * Possible values: 'term_id', 'slug' or 'name' - * Default: 'slug' - * - 'operator' string (optional) - * Possible values: 'IN' and 'NOT IN'. - * Default: 'IN' - * - 'include_children' bool (optional) Whether to include child terms. - * Default: true - * - * @param string $object_id_column - * @return string - */ - function get_tax_sql( $tax_query, $object_id_column ) { - global $wpdb; - - $sql = array(); - foreach ( $tax_query as $query ) { - if ( !isset( $query['include_children'] ) ) - $query['include_children'] = true; - - $query['do_query'] = false; - - $sql_single = get_objects_in_term( $query['terms'], $query['taxonomy'], $query ); - - if ( empty( $sql_single ) ) - return ' AND 0 = 1'; - - $sql[] = $sql_single; - } - - if ( 1 == count( $sql ) ) { - $ids = $wpdb->get_col( $sql[0] ); - } else { - $r = "SELECT object_id FROM $wpdb->term_relationships WHERE 1=1"; - foreach ( $sql as $query ) - $r .= " AND object_id IN ($query)"; - - $ids = $wpdb->get_col( $r ); - } - - if ( !empty( $ids ) ) - return " AND $object_id_column IN(" . implode( ', ', $ids ) . ")"; - else - return ' AND 0 = 1'; - } - /* * Used internally to generate an SQL string for searching across multiple columns * diff --git a/wp-includes/query.php b/wp-includes/query.php index cc7c2379ad..c90c3ec0d7 100644 --- a/wp-includes/query.php +++ b/wp-includes/query.php @@ -1899,7 +1899,7 @@ class WP_Query extends WP_Object_Query { $post_status_join = true; } - $where .= $this->get_tax_sql( $q['tax_query'], "$wpdb->posts.ID" ); + $where .= get_tax_sql( $q['tax_query'], "$wpdb->posts.ID" ); // Back-compat $cat_query = wp_list_filter( $q['tax_query'], array( 'taxonomy' => 'category' ) ); diff --git a/wp-includes/taxonomy.php b/wp-includes/taxonomy.php index da38f049ad..07afbd6e62 100644 --- a/wp-includes/taxonomy.php +++ b/wp-includes/taxonomy.php @@ -554,6 +554,61 @@ function get_objects_in_term( $terms, $taxonomies, $args = array() ) { return $do_query ? $wpdb->get_col( $sql ) : $sql; } +/* + * Given a meta query, generates SQL to be appended to a main query + * + * @since 3.1.0 + * + * @param array $tax_query List of taxonomy queries. A single taxonomy query is an associative array: + * - 'taxonomy' string|array The taxonomy being queried + * - 'terms' string|array The list of terms + * - 'field' string (optional) Which term field is being used. + * Possible values: 'term_id', 'slug' or 'name' + * Default: 'slug' + * - 'operator' string (optional) + * Possible values: 'IN' and 'NOT IN'. + * Default: 'IN' + * - 'include_children' bool (optional) Whether to include child terms. + * Default: true + * + * @param string $object_id_column + * @return string + */ +function get_tax_sql( $tax_query, $object_id_column ) { + global $wpdb; + + $sql = array(); + foreach ( $tax_query as $query ) { + if ( !isset( $query['include_children'] ) ) + $query['include_children'] = true; + + $query['do_query'] = false; + + $sql_single = get_objects_in_term( $query['terms'], $query['taxonomy'], $query ); + + if ( empty( $sql_single ) ) + return ' AND 0 = 1'; + + $sql[] = $sql_single; + } + + if ( 1 == count( $sql ) ) { + $ids = $wpdb->get_col( $sql[0] ); + } else { + $r = "SELECT object_id FROM $wpdb->term_relationships WHERE 1=1"; + foreach ( $sql as $query ) + $r .= " AND object_id IN ($query)"; + + $ids = $wpdb->get_col( $r ); + } + + if ( !empty( $ids ) ) + return " AND $object_id_column IN(" . implode( ', ', $ids ) . ")"; + else + return ' AND 0 = 1'; +} + + /** * Get all Term data from database by Term ID. *