Make WordPress Core


Ignore:
Timestamp:
11/25/2024 11:14:37 AM (5 months 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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.