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:3233
Prepares the 'tax_query' for a collection of posts.

Compatibility

WordPress
since 5.7.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

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

Return value

array
Updated query arguments.

Performance profile

How much work a call to WP_REST_Posts_Controller::prepare_tax_query() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Light

Touches nothing outside its own arguments.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
18–22

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 103.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
1

1 place in core call this, so the cost is paid more often than your own code shows.

What one call costs · 1 distinct outcome

One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_REST_Posts_Controller::prepare_tax_query() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always18–22get_object_taxonomies(), wp_list_filter()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev10318–2216
8.510318–2216
8.410318–2216
8.310318–2216
8.210318–2216
8.110318–22164 fewer instructions than PHP 7.4
7.410718–2216

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

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 code

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

Changelog

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 7.1.0 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.