From 44c4b365a0021e356b144cb66c80702aa081dd18 Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Fri, 23 May 2014 19:59:14 +0000 Subject: [PATCH] When adding queries to `tax_query`: if the query's `field` is `term_taxonomy_id`, don't require `taxonomy` to be specified. In `WP_Tax_Query::transform_query()`, `$query['taxonomy']` is never checked for the 'term_taxonomy_id' `case` because 'term_taxonomy_id' is the primary key being looked up. Adds unit tests. Props helen. Fixes #25284. Built from https://develop.svn.wordpress.org/trunk@28562 git-svn-id: http://core.svn.wordpress.org/trunk@28388 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/taxonomy.php | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/wp-includes/taxonomy.php b/wp-includes/taxonomy.php index 3b72a9cbc7..b0f70d0e8c 100644 --- a/wp-includes/taxonomy.php +++ b/wp-includes/taxonomy.php @@ -632,15 +632,15 @@ class WP_Tax_Query { /** * List of taxonomy queries. A single taxonomy query is an associative array: - * - 'taxonomy' string The taxonomy being queried + * - 'taxonomy' string The taxonomy being queried. Optional when using the term_taxonomy_id field. * - 'terms' string|array The list of terms * - 'field' string (optional) Which term field is being used. - * Possible values: 'term_id', 'slug' or 'name' + * Possible values: 'term_id', 'slug', 'name', or 'term_taxonomy_id' * Default: 'term_id' * - 'operator' string (optional) * Possible values: 'AND', 'IN' or 'NOT IN'. * Default: 'IN' - * - 'include_children' bool (optional) Whether to include child terms. + * - 'include_children' bool (optional) Whether to include child terms. Requires that a taxonomy be specified. * Default: true * * @since 3.1.0 @@ -818,7 +818,15 @@ class WP_Tax_Query { * @param array &$query The single query */ private function clean_query( &$query ) { - if ( ! taxonomy_exists( $query['taxonomy'] ) ) { + if ( empty( $query['taxonomy'] ) ) { + if ( 'term_taxonomy_id' !== $query['field'] ) { + $query = new WP_Error( 'Invalid taxonomy' ); + return; + } + + // so long as there are shared terms, include_children requires that a taxonomy is set + $query['include_children'] = false; + } elseif ( ! taxonomy_exists( $query['taxonomy'] ) ) { $query = new WP_Error( 'Invalid taxonomy' ); return; }