Make WordPress Core


Ignore:
Timestamp:
05/26/2021 02:15:51 AM (5 years ago)
Author:
TimothyBlynJacobs
Message:

REST API: Add support for modifying the term relation when querying posts.

By default, a post most contain any of the requested terms to be included in the response. This commit adds a new operator property that can be set to AND to require a post to contain all of the requested terms.

For example, /wp/v2/posts?tags[terms]=1,2,3&tags[operator]=AND will return posts that have tags with the ids of 1, 2, and 3.

Props dlh, earnjam, Clorith, jnylen0, sebbb.
Fixes #41287.

File:
1 edited

Legend:

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

    r51000 r51026  
    29422942                                $terms            = array();
    29432943                                $include_children = false;
     2944                                $operator         = 'IN';
    29442945
    29452946                                if ( rest_is_array( $tax_include ) ) {
    … …  
    29482949                                        $terms            = empty( $tax_include['terms'] ) ? array() : $tax_include['terms'];
    29492950                                        $include_children = ! empty( $tax_include['include_children'] );
     2951
     2952                                        if ( isset( $tax_include['operator'] ) && 'AND' === $tax_include['operator'] ) {
     2953                                                $operator = 'AND';
     2954                                        }
    29502955                                }
    29512956
    … …  
    29562961                                                'terms'            => $terms,
    29572962                                                'include_children' => $include_children,
     2963                                                'operator'         => $operator,
    29582964                                        );
    29592965                                }
    … …  
    30493055                        $limit_schema
    30503056                );
     3057                // 'operator' is supported only for 'include' queries.
     3058                $include_schema['oneOf'][1]['properties']['operator'] = array(
     3059                        'description' => __( 'Whether items must be assigned all or any of the specified terms.' ),
     3060                        'type'        => 'string',
     3061                        'enum'        => array( 'AND', 'OR' ),
     3062                        'default'     => 'OR',
     3063                );
     3064
    30513065                $exclude_schema = array_merge(
    30523066                        array(
Note: See TracChangeset for help on using the changeset viewer.