In `get_terms()`, check hierarchy for all `$taxonomies` before bailing early from 'parent' or 'child_of'.

There is a pre-check in `get_terms()` that prevents an unnecessary database
query if the 'parent' or 'child_of' parameter is not found in the cached term
hierarchy (since a term without an index in the hierarchy cache has no
descendants). Previously, only the first item in the `$taxonomies` array was
being checked, with the result that an empty array was being erroneously
returned in cases where the 'parent' or 'child_of' term is in a subsequent
taxonomy.

See #31118.
Built from https://develop.svn.wordpress.org/trunk@31276


git-svn-id: http://core.svn.wordpress.org/trunk@31257 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Boone Gorges 2015-01-25 02:46:25 +00:00
parent 43fceb39c0
commit acef0e2dcc
2 changed files with 25 additions and 12 deletions

View File

@ -1657,18 +1657,29 @@ function get_terms( $taxonomies, $args = '' ) {
*/ */
$args = apply_filters( 'get_terms_args', $args, $taxonomies ); $args = apply_filters( 'get_terms_args', $args, $taxonomies );
// Avoid the query if the queried parent/child_of term has no descendants.
$child_of = $args['child_of']; $child_of = $args['child_of'];
$parent = $args['parent'];
if ( $child_of ) { if ( $child_of ) {
$hierarchy = _get_term_hierarchy( reset( $taxonomies ) ); $_parent = $child_of;
if ( ! isset( $hierarchy[ $child_of ] ) ) { } elseif ( $parent ) {
return $empty_array; $_parent = $parent;
} else {
$_parent = false;
}
if ( $_parent ) {
$in_hierarchy = false;
foreach ( $taxonomies as $_tax ) {
$hierarchy = _get_term_hierarchy( $_tax );
if ( isset( $hierarchy[ $_parent ] ) ) {
$in_hierarchy = true;
} }
} }
$parent = $args['parent']; if ( ! $in_hierarchy ) {
if ( $parent ) {
$hierarchy = _get_term_hierarchy( reset( $taxonomies ) );
if ( ! isset( $hierarchy[ $parent ] ) ) {
return $empty_array; return $empty_array;
} }
} }
@ -1942,9 +1953,11 @@ function get_terms( $taxonomies, $args = '' ) {
} }
if ( $child_of ) { if ( $child_of ) {
$children = _get_term_hierarchy( reset( $taxonomies ) ); foreach ( $taxonomies as $_tax ) {
$children = _get_term_hierarchy( $_tax );
if ( ! empty( $children ) ) { if ( ! empty( $children ) ) {
$terms = _get_term_children( $child_of, $terms, reset( $taxonomies ) ); $terms = _get_term_children( $child_of, $terms, $_tax );
}
} }
} }

View File

@ -4,7 +4,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '4.2-alpha-31275'; $wp_version = '4.2-alpha-31276';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.