Make WordPress Core

Changeset 40994


Ignore:
Timestamp:
07/02/2017 06:28:21 PM (9 years ago)
Author:
boonebgorges
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/taxonomy.php

    r40984 r40994  
    18831883        $args = wp_parse_args( $args );
    18841884
     1885        /**
     1886         * Filter arguments for retrieving object terms.
     1887         *
     1888         * @since 4.9.0
     1889         *
     1890         * @param array        $args       An array of arguments for retrieving terms for the given object(s).
     1891         *                                 See {@see wp_get_object_terms()} for details.
     1892         * @param int|array    $object_ids Object ID or array of IDs.
     1893         * @param string|array $taxonomies The taxonomies to retrieve terms from.
     1894         */
     1895        $args = apply_filters( 'wp_get_object_terms_args', $args, $object_ids, $taxonomies );
     1896
    18851897        /*
    18861898         * When one or more queried taxonomies is registered with an 'args' array,
  • trunk/tests/phpunit/tests/term/wpGetObjectTerms.php

    r40290 r40994  
    733733                $this->assertEquals( array( $t1, $t3, $t2 ), $found );
    734734        }
     735
     736        /**
     737         * @ticket 35925
     738         */
     739        public function test_wp_get_object_terms_args_filter() {
     740                $taxonomy = 'wptests_tax_4';
     741
     742                register_taxonomy( $taxonomy, 'post', array( 'sort' => 'true' ) );
     743                $post_id = self::factory()->post->create();
     744                $terms = array( 'foo', 'bar', 'baz' );
     745                $set = wp_set_object_terms( $post_id, $terms, $taxonomy );
     746
     747                // Filter for maintaining term order
     748                add_filter( 'wp_get_object_terms_args', array( $this, 'filter_wp_get_object_terms_args' ), 10, 3 );
     749
     750                // Test directly
     751                $get_object_terms = wp_get_object_terms( $post_id, $taxonomy, array( 'fields' => 'names' ) );
     752                $this->assertEquals( $terms, $get_object_terms );
     753
     754                // Test metabox taxonomy (admin advanced edit)
     755                $terms_to_edit = get_terms_to_edit( $post_id, $taxonomy );
     756                $this->assertEquals( implode( ',', $terms ), $terms_to_edit );
     757        }
     758
     759        function filter_wp_get_object_terms_args ( $args, $object_ids, $taxonomies ) {
     760                $args['orderby'] = 'term_order';
     761                return $args;
     762        }
    735763}
Note: See TracChangeset for help on using the changeset viewer.