WordPress at Your Fingertips
function is not described

WP_REST_Posts_Controller::prepare_tax_query() private WP 5.7.0

Prepares the 'tax_query' for a collection of posts.

{} It's a method of the class: WP_REST_Posts_Controller{}

No Hooks.

Return

Array. Updated query arguments.

Usage

// private - for code of main (parent) class only
$result = $this->prepare_tax_query( $args, $request );
$args(array) (required)
WP_Query arguments.
$request(WP_REST_Request) (required)
Full details about the request.

Changelog

Since 5.7.0 Introduced.

Code of WP_REST_Posts_Controller::prepare_tax_query() WP 5.7.2

<?php
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;

			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 ( $terms ) {
				$args['tax_query'][] = array(
					'taxonomy'         => $taxonomy->name,
					'field'            => 'term_id',
					'terms'            => $terms,
					'include_children' => $include_children,
				);
			}
		}

		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;
}