wppaste
WordPress

WP_REST_Posts_Controller::prepare_tax_query( array $args, WP_REST_Request $request ): array

Since
5.7.0
Source
wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php:3174
Prepares the 'tax_query' for a collection of posts.

Parameters

$argsarray
WP_Query arguments.
$requestWP_REST_Request
Full details about the request.

Return

array
Updated query arguments.

Uses · 4

  • wp_list_filter()Filters a list of objects, based on a set of key => value arguments.
  • get_object_taxonomies()Returns the names or objects of the taxonomies which are registered for the requested object or object type, such as a post object or post type name.
  • rest_is_array()Determines if a given value is array-like.
  • rest_is_object()Determines if a given value is object-like.

Used by · 1

Source

	private function prepare_tax_query( array $args, WP_REST_Request $request ) {		$relation = $request['tax_relation']; 		if ( $relation ) {			$args['tax_query'] = array( 'relation' => $relation );		} 		$taxonomies = wp_list_filter(			get_object_taxonomies( $this->post_type, 'objects' ),			array( 'show_in_rest' => true )		); 		foreach ( $taxonomies as $taxonomy ) {			$base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name; 			$tax_include = $request[ $base ];			$tax_exclude = $request[ $base . '_exclude' ]; 			if ( $tax_include ) {				$terms            = array();				$include_children = false;				$operator         = 'IN'; 				if ( rest_is_array( $tax_include ) ) {					$terms = $tax_include;				} elseif ( rest_is_object( $tax_include ) ) {					$terms            = empty( $tax_include['terms'] ) ? array() : $tax_include['terms'];					$include_children = ! empty( $tax_include['include_children'] ); 					if ( isset( $tax_include['operator'] ) && 'AND' === $tax_include['operator'] ) {						$operator = 'AND';					}				} 				if ( $terms ) {					$args['tax_query'][] = array(						'taxonomy'         => $taxonomy->name,						'field'            => 'term_id',						'terms'            => $terms,						'include_children' => $include_children,						'operator'         => $operator,					);				}			} 			if ( $tax_exclude ) {				$terms            = array();				$include_children = false; 				if ( rest_is_array( $tax_exclude ) ) {					$terms = $tax_exclude;				} elseif ( rest_is_object( $tax_exclude ) ) {					$terms            = empty( $tax_exclude['terms'] ) ? array() : $tax_exclude['terms'];					$include_children = ! empty( $tax_exclude['include_children'] );				} 				if ( $terms ) {					$args['tax_query'][] = array(						'taxonomy'         => $taxonomy->name,						'field'            => 'term_id',						'terms'            => $terms,						'include_children' => $include_children,						'operator'         => 'NOT IN',					);				}			}		} 		return $args;	}

History

Introduced in 5.7.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.9.7 tag, from src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.