Make WordPress Core

Changeset 59458


Ignore:
Timestamp:
11/25/2024 11:14:37 AM (2 years ago)
Author:
Bernhard Reiter
Message:

REST API: Terms: Respect taxonomy's default query args.

It is possible to supply a set of default query args to register_taxonomy() which will be used when querying a list of terms -- for example, orderby in order to specify how the resulting list of terms should be sorted.

The Terms REST API controller previously respected these default query args only if the request included a post ID. This changeset makes it so that the default args will also be respected if no post ID is provided.

Props bernhard-reiter, jsnajdr.
Fixes #62500.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php

    r58706 r59458  
    228228         *
    229229         * @since 4.7.0
     230         * @since 6.8.0 Respect default query arguments set for the taxonomy upon registration.
    230231         *
    231232         * @param WP_REST_Request $request Full details about the request.
    … …  
    294295                                }
    295296                        }
     297                }
     298
     299                /*
     300                 * When a taxonomy is registered with an 'args' array,
     301                 * those params override the `$args` passed to this function.
     302                 *
     303                 * We only need to do this if no `post` argument is provided.
     304                 * Otherwise, terms will be fetched using `wp_get_object_terms()`,
     305                 * which respects the default query arguments set for the taxonomy.
     306                 */
     307                if (
     308                        empty( $prepared_args['post'] ) &&
     309                        isset( $taxonomy_obj->args ) &&
     310                        is_array( $taxonomy_obj->args )
     311                ) {
     312                        $prepared_args = array_merge( $prepared_args, $taxonomy_obj->args );
    296313                }
    297314
  • trunk/tests/phpunit/tests/rest-api/rest-tags-controller.php

    r56746 r59458  
    481481                $this->assertCount( 2, $data );
    482482                $this->assertSame( 'Cape', $data[0]['name'] );
     483        }
     484
     485        /**
     486         * @ticket 62500
     487         */
     488        public function test_get_items_custom_tax_without_post_arg_respects_tax_query_args() {
     489                register_taxonomy(
     490                        'batman',
     491                        'post',
     492                        array(
     493                                'show_in_rest' => true,
     494                                'sort'         => true,
     495                                'args'         => array(
     496                                        'order'   => 'DESC',
     497                                        'orderby' => 'name',
     498                                ),
     499                        )
     500                );
     501                $controller = new WP_REST_Terms_Controller( 'batman' );
     502                $controller->register_routes();
     503                $term1 = self::factory()->term->create(
     504                        array(
     505                                'name'     => 'Cycle',
     506                                'taxonomy' => 'batman',
     507                        )
     508                );
     509                $term2 = self::factory()->term->create(
     510                        array(
     511                                'name'     => 'Pod',
     512                                'taxonomy' => 'batman',
     513                        )
     514                );
     515                $term3 = self::factory()->term->create(
     516                        array(
     517                                'name'     => 'Cave',
     518                                'taxonomy' => 'batman',
     519                        )
     520                );
     521
     522                $request  = new WP_REST_Request( 'GET', '/wp/v2/batman' );
     523                $response = rest_get_server()->dispatch( $request );
     524                $this->assertSame( 200, $response->get_status() );
     525
     526                $data = $response->get_data();
     527                $this->assertCount( 3, $data );
     528                $this->assertSame(
     529                        array( 'Pod', 'Cycle', 'Cave' ),
     530                        array_column( $data, 'name' )
     531                );
    483532        }
    484533
Note: See TracChangeset for help on using the changeset viewer.