Restore support for taxonomy 'args' override when querying object terms.
[7520] introduced an undocumented feature whereby developers could register a custom taxonomy with an 'args' parameter, consisting of an array of config params that, when present, override corresponding params in the `$args` array passed to `wp_get_object_terms()` when using that function to query for terms in the specified taxonomy. The `wp_get_object_terms()` refactor in [38667] failed to respect this secret covenant, and the current changeset atones for the transgression. Props danielbachhuber. Fixes #40496. git-svn-id: https://develop.svn.wordpress.org/trunk@40513 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
6ea3026565
commit
ccd942ddaa
@ -1841,10 +1841,30 @@ function wp_get_object_terms($object_ids, $taxonomies, $args = array()) {
|
||||
|
||||
$args = wp_parse_args( $args );
|
||||
|
||||
/*
|
||||
* When one or more queried taxonomies is registered with an 'args' array,
|
||||
* those params override the `$args` passed to this function.
|
||||
*/
|
||||
$terms = array();
|
||||
if ( count( $taxonomies ) > 1 ) {
|
||||
foreach ( $taxonomies as $index => $taxonomy ) {
|
||||
$t = get_taxonomy( $taxonomy );
|
||||
if ( isset( $t->args ) && is_array( $t->args ) && $args != array_merge( $args, $t->args ) ) {
|
||||
unset( $taxonomies[ $index ] );
|
||||
$terms = array_merge( $terms, wp_get_object_terms( $object_ids, $taxonomy, array_merge( $args, $t->args ) ) );
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$t = get_taxonomy( $taxonomies[0] );
|
||||
if ( isset( $t->args ) && is_array( $t->args ) ) {
|
||||
$args = array_merge( $args, $t->args );
|
||||
}
|
||||
}
|
||||
|
||||
$args['taxonomy'] = $taxonomies;
|
||||
$args['object_ids'] = $object_ids;
|
||||
|
||||
$terms = get_terms( $args );
|
||||
$terms = array_merge( $terms, get_terms( $args ) );
|
||||
|
||||
/**
|
||||
* Filters the terms for a given object or objects.
|
||||
|
@ -381,4 +381,50 @@ class Tests_Term_Query extends WP_UnitTestCase {
|
||||
$count = $query->get_terms();
|
||||
$this->assertEquals( 1, $count );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 40496
|
||||
*/
|
||||
public function test_get_the_terms_should_respect_taxonomy_orderby() {
|
||||
register_taxonomy( 'wptests_tax', 'post', array(
|
||||
'sort' => true,
|
||||
'args' => array(
|
||||
'orderby' => 'term_order',
|
||||
),
|
||||
) );
|
||||
$term_ids = self::factory()->term->create_many( 2, array(
|
||||
'taxonomy' => 'wptests_tax',
|
||||
) );
|
||||
$post_id = self::factory()->post->create();
|
||||
wp_set_object_terms( $post_id, array( $term_ids[0], $term_ids[1] ), 'wptests_tax' );
|
||||
$terms = get_the_terms( $post_id, 'wptests_tax' );
|
||||
$this->assertEquals( array( $term_ids[0], $term_ids[1] ), wp_list_pluck( $terms, 'term_id' ) );
|
||||
// Flip the order
|
||||
wp_set_object_terms( $post_id, array( $term_ids[1], $term_ids[0] ), 'wptests_tax' );
|
||||
$terms = get_the_terms( $post_id, 'wptests_tax' );
|
||||
$this->assertEquals( array( $term_ids[1], $term_ids[0] ), wp_list_pluck( $terms, 'term_id' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 40496
|
||||
*/
|
||||
public function test_wp_get_object_terms_should_respect_taxonomy_orderby() {
|
||||
register_taxonomy( 'wptests_tax', 'post', array(
|
||||
'sort' => true,
|
||||
'args' => array(
|
||||
'orderby' => 'term_order',
|
||||
),
|
||||
) );
|
||||
$term_ids = self::factory()->term->create_many( 2, array(
|
||||
'taxonomy' => 'wptests_tax',
|
||||
) );
|
||||
$post_id = self::factory()->post->create();
|
||||
wp_set_object_terms( $post_id, array( $term_ids[0], $term_ids[1] ), 'wptests_tax' );
|
||||
$terms = wp_get_object_terms( $post_id, array( 'category', 'wptests_tax' ) );
|
||||
$this->assertEquals( array( $term_ids[0], $term_ids[1], 1 ), wp_list_pluck( $terms, 'term_id' ) );
|
||||
// Flip the order
|
||||
wp_set_object_terms( $post_id, array( $term_ids[1], $term_ids[0] ), 'wptests_tax' );
|
||||
$terms = wp_get_object_terms( $post_id, array( 'category', 'wptests_tax' ) );
|
||||
$this->assertEquals( array( $term_ids[1], $term_ids[0], 1 ), wp_list_pluck( $terms, 'term_id' ) );
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user