From a16d16449ddee6a3c13a65fd02536a6071c04d70 Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Sun, 2 Jul 2017 18:28:21 +0000 Subject: [PATCH] Introduce `wp_get_object_terms_args` filter. This filter allows developers to modify the arguments passed to `wp_get_object_terms()` before the query is run. Props enrico.sorcinelli. Fixes #35925. git-svn-id: https://develop.svn.wordpress.org/trunk@40994 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/taxonomy.php | 12 ++++++++ tests/phpunit/tests/term/wpGetObjectTerms.php | 28 +++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/src/wp-includes/taxonomy.php b/src/wp-includes/taxonomy.php index eb07898507..05c6a10e8e 100644 --- a/src/wp-includes/taxonomy.php +++ b/src/wp-includes/taxonomy.php @@ -1882,6 +1882,18 @@ function wp_get_object_terms($object_ids, $taxonomies, $args = array()) { $args = wp_parse_args( $args ); + /** + * Filter arguments for retrieving object terms. + * + * @since 4.9.0 + * + * @param array $args An array of arguments for retrieving terms for the given object(s). + * See {@see wp_get_object_terms()} for details. + * @param int|array $object_ids Object ID or array of IDs. + * @param string|array $taxonomies The taxonomies to retrieve terms from. + */ + $args = apply_filters( 'wp_get_object_terms_args', $args, $object_ids, $taxonomies ); + /* * When one or more queried taxonomies is registered with an 'args' array, * those params override the `$args` passed to this function. diff --git a/tests/phpunit/tests/term/wpGetObjectTerms.php b/tests/phpunit/tests/term/wpGetObjectTerms.php index f68e4f674d..e527a601bf 100644 --- a/tests/phpunit/tests/term/wpGetObjectTerms.php +++ b/tests/phpunit/tests/term/wpGetObjectTerms.php @@ -732,4 +732,32 @@ class Tests_Term_WpGetObjectTerms extends WP_UnitTestCase { $this->assertEquals( array( $t1, $t3, $t2 ), $found ); } + + /** + * @ticket 35925 + */ + public function test_wp_get_object_terms_args_filter() { + $taxonomy = 'wptests_tax_4'; + + register_taxonomy( $taxonomy, 'post', array( 'sort' => 'true' ) ); + $post_id = self::factory()->post->create(); + $terms = array( 'foo', 'bar', 'baz' ); + $set = wp_set_object_terms( $post_id, $terms, $taxonomy ); + + // Filter for maintaining term order + add_filter( 'wp_get_object_terms_args', array( $this, 'filter_wp_get_object_terms_args' ), 10, 3 ); + + // Test directly + $get_object_terms = wp_get_object_terms( $post_id, $taxonomy, array( 'fields' => 'names' ) ); + $this->assertEquals( $terms, $get_object_terms ); + + // Test metabox taxonomy (admin advanced edit) + $terms_to_edit = get_terms_to_edit( $post_id, $taxonomy ); + $this->assertEquals( implode( ',', $terms ), $terms_to_edit ); + } + + function filter_wp_get_object_terms_args ( $args, $object_ids, $taxonomies ) { + $args['orderby'] = 'term_order'; + return $args; + } }