wppaste
WordPress

WP_Tax_Query::sanitize_query( array $queries ): array

Since
4.1.0
Source
wp-includes/class-wp-tax-query.php:136
Ensures the 'tax_query' argument passed to the class constructor is well-formed.

Description

Ensures that each query-level clause has a 'relation' key, and that each first-order clause contains all the necessary keys from $defaults.

Parameters

$queriesarray
Array of queries clauses.

Return

array
Sanitized array of query clauses.

Uses · 3

Used by · 2

Source

	public function sanitize_query( $queries ) {		$cleaned_query = array(); 		$defaults = array(			'taxonomy'         => '',			'terms'            => array(),			'field'            => 'term_id',			'operator'         => 'IN',			'include_children' => true,		); 		foreach ( $queries as $key => $query ) {			if ( 'relation' === $key ) {				$cleaned_query['relation'] = $this->sanitize_relation( $query ); 				// First-order clause.			} elseif ( self::is_first_order_clause( $query ) ) { 				$cleaned_clause          = array_merge( $defaults, $query );				$cleaned_clause['terms'] = (array) $cleaned_clause['terms'];				$cleaned_query[]         = $cleaned_clause; 				/*				 * Keep a copy of the clause in the flate				 * $queried_terms array, for use in WP_Query.				 */				if ( ! empty( $cleaned_clause['taxonomy'] ) && 'NOT IN' !== $cleaned_clause['operator'] ) {					$taxonomy = $cleaned_clause['taxonomy'];					if ( ! isset( $this->queried_terms[ $taxonomy ] ) ) {						$this->queried_terms[ $taxonomy ] = array();					} 					/*					 * Backward compatibility: Only store the first					 * 'terms' and 'field' found for a given taxonomy.					 */					if ( ! empty( $cleaned_clause['terms'] ) && ! isset( $this->queried_terms[ $taxonomy ]['terms'] ) ) {						$this->queried_terms[ $taxonomy ]['terms'] = $cleaned_clause['terms'];					} 					if ( ! empty( $cleaned_clause['field'] ) && ! isset( $this->queried_terms[ $taxonomy ]['field'] ) ) {						$this->queried_terms[ $taxonomy ]['field'] = $cleaned_clause['field'];					}				} 				// Otherwise, it's a nested query, so we recurse.			} elseif ( is_array( $query ) ) {				$cleaned_subquery = $this->sanitize_query( $query ); 				if ( ! empty( $cleaned_subquery ) ) {					// All queries with children must have a relation.					if ( ! isset( $cleaned_subquery['relation'] ) ) {						$cleaned_subquery['relation'] = 'AND';					} 					$cleaned_query[] = $cleaned_subquery;				}			}		} 		return $cleaned_query;	}

History

Introduced in 4.1.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.0.4 tag, from src/wp-includes/class-wp-tax-query.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.