From 5124c542e18add055163e8f786275fbb1f9c2df5 Mon Sep 17 00:00:00 2001 From: TimothyBlynJacobs Date: Sun, 3 Nov 2019 23:14:02 +0000 Subject: [PATCH] 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. Built from https://develop.svn.wordpress.org/trunk@46646 git-svn-id: http://core.svn.wordpress.org/trunk@46446 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- .../endpoints/class-wp-rest-posts-controller.php | 12 ++++++++++++ wp-includes/version.php | 2 +- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php b/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php index a0d3fe805d..d0333c61c4 100644 --- a/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php +++ b/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php @@ -270,6 +270,10 @@ class WP_REST_Posts_Controller extends WP_REST_Controller { $taxonomies = wp_list_filter( get_object_taxonomies( $this->post_type, 'objects' ), array( 'show_in_rest' => true ) ); + if ( ! empty( $request['tax_relation'] ) ) { + $query_args['tax_query'] = array( 'relation' => $request['tax_relation'] ); + } + foreach ( $taxonomies as $taxonomy ) { $base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name; $tax_exclude = $base . '_exclude'; @@ -2531,6 +2535,14 @@ class WP_REST_Posts_Controller extends WP_REST_Controller { $taxonomies = wp_list_filter( get_object_taxonomies( $this->post_type, 'objects' ), array( 'show_in_rest' => true ) ); + if ( ! empty( $taxonomies ) ) { + $query_params['tax_relation'] = array( + 'description' => __( 'Limit result set based on relationship between multiple taxonomies.' ), + 'type' => 'string', + 'enum' => array( 'AND', 'OR' ), + ); + } + foreach ( $taxonomies as $taxonomy ) { $base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name; diff --git a/wp-includes/version.php b/wp-includes/version.php index 530cc08646..c26f152664 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -13,7 +13,7 @@ * * @global string $wp_version */ -$wp_version = '5.4-alpha-46645'; +$wp_version = '5.4-alpha-46646'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.