WordPress.org

Make WordPress Core


Ignore:
Timestamp:
11/03/2019 11:12:44 PM (13 months 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/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

    r46609 r46646  
    270270
    271271        $taxonomies = wp_list_filter( get_object_taxonomies( $this->post_type, 'objects' ), array( 'show_in_rest' => true ) );
     272
     273        if ( ! empty( $request['tax_relation'] ) ) {
     274            $query_args['tax_query'] = array( 'relation' => $request['tax_relation'] );
     275        }
    272276
    273277        foreach ( $taxonomies as $taxonomy ) {
     
    25322536        $taxonomies = wp_list_filter( get_object_taxonomies( $this->post_type, 'objects' ), array( 'show_in_rest' => true ) );
    25332537
     2538        if ( ! empty( $taxonomies ) ) {
     2539            $query_params['tax_relation'] = array(
     2540                'description' => __( 'Limit result set based on relationship between multiple taxonomies.' ),
     2541                'type'        => 'string',
     2542                'enum'        => array( 'AND', 'OR' ),
     2543            );
     2544        }
     2545
    25342546        foreach ( $taxonomies as $taxonomy ) {
    25352547            $base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name;
Note: See TracChangeset for help on using the changeset viewer.