Make WordPress Core


Ignore:
Timestamp:
11/03/2019 11:12:44 PM (5 years ago)
Author:
TimothyBlynJacobs
Message:

REST API: Add tax relation parameter to posts collection.

The REST API supports filtering by terms across multiple taxonomies using an AND relation. This adds support for an OR relation by adding "tax_relation=OR" as a query parameter.

Props earnjam.
Fixes #44326.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/rest-api/rest-posts-controller.php

    r46586 r46646  
    176176                'tags',
    177177                'tags_exclude',
     178                'tax_relation',
    178179            ),
    179180            $keys
     
    960961    }
    961962
     963    /**
     964     * @ticket 44326
     965     */
     966    public function test_get_items_tags_or_categories_query() {
     967        $id1      = self::$post_id;
     968        $id2      = $this->factory->post->create( array( 'post_status' => 'publish' ) );
     969        $id3      = $this->factory->post->create( array( 'post_status' => 'publish' ) );
     970        $id4      = $this->factory->post->create( array( 'post_status' => 'publish' ) );
     971        $tag      = wp_insert_term( 'My Tag', 'post_tag' );
     972        $category = wp_insert_term( 'My Category', 'category' );
     973
     974        wp_set_object_terms( $id1, array( $tag['term_id'] ), 'post_tag' );
     975        wp_set_object_terms( $id2, array( $category['term_id'] ), 'category' );
     976
     977        $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
     978        $request->set_param( 'tax_relation', 'OR' );
     979        $request->set_param( 'tags', array( $tag['term_id'] ) );
     980        $request->set_param( 'categories', array( $category['term_id'] ) );
     981
     982        $response = rest_get_server()->dispatch( $request );
     983        $data     = $response->get_data();
     984        $this->assertCount( 2, $data );
     985        $this->assertEquals( $id2, $data[0]['id'] );
     986        $this->assertEquals( $id1, $data[1]['id'] );
     987    }
     988
    962989    public function test_get_items_tags_and_categories_exclude_query() {
    963990        $id1      = self::$post_id;
     
    9841011        $response = rest_get_server()->dispatch( $request );
    9851012        $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
     1013    }
     1014
     1015    /**
     1016     * @ticket 44326
     1017     */
     1018    public function test_get_items_tags_or_categories_exclude_query() {
     1019        $id1      = self::$post_id;
     1020        $id2      = $this->factory->post->create( array( 'post_status' => 'publish' ) );
     1021        $id3      = $this->factory->post->create( array( 'post_status' => 'publish' ) );
     1022        $id4      = $this->factory->post->create( array( 'post_status' => 'publish' ) );
     1023        $tag      = wp_insert_term( 'My Tag', 'post_tag' );
     1024        $category = wp_insert_term( 'My Category', 'category' );
     1025
     1026        wp_set_object_terms( $id1, array( $tag['term_id'] ), 'post_tag' );
     1027        wp_set_object_terms( $id2, array( $tag['term_id'] ), 'post_tag' );
     1028        wp_set_object_terms( $id2, array( $category['term_id'] ), 'category' );
     1029        wp_set_object_terms( $id3, array( $category['term_id'] ), 'category' );
     1030
     1031        $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
     1032        $request->set_param( 'tags', array( $tag['term_id'] ) );
     1033        $request->set_param( 'categories_exclude', array( $category['term_id'] ) );
     1034        $request->set_param( 'tax_relation', 'OR' );
     1035
     1036        $response = rest_get_server()->dispatch( $request );
     1037        $data     = $response->get_data();
     1038        $this->assertCount( 3, $data );
     1039        $this->assertEquals( $id2, $data[0]['id'] );
     1040        $this->assertEquals( $id4, $data[1]['id'] );
     1041        $this->assertEquals( $id1, $data[2]['id'] );
     1042    }
     1043
     1044    /**
     1045     * @ticket 44326
     1046     */
     1047    public function test_get_items_relation_with_no_tax_query() {
     1048        $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
     1049        $request->set_param( 'tax_relation', 'OR' );
     1050        $request->set_param( 'include', self::$post_id );
     1051
     1052        $response = rest_get_server()->dispatch( $request );
     1053        $this->assertEquals( 200, $response->get_status() );
     1054        $this->assertCount( 1, $response->get_data() );
     1055        $this->assertEquals( self::$post_id, $response->get_data()[0]['id'] );
    9861056    }
    9871057
Note: See TracChangeset for help on using the changeset viewer.