WP_Tax_Query::sanitize_query() public WP 4.1.0
Ensure the 'tax_query' argument passed to the class constructor is well-formed.
Ensures that each query-level clause has a 'relation' key, and that each first-order clause contains all the necessary keys from $defaults.
{} Это метод класса: WP_Tax_Query{}
Хуков нет.
Возвращает
Массив
. Sanitized array of query clauses.
Использование
$WP_Tax_Query = new WP_Tax_Query(); $WP_Tax_Query->sanitize_query( $queries );
- $queries(массив) (обязательный)
- Array of queries clauses.
Список изменений
С версии 4.1.0 | Введена. |
Код WP_Tax_Query::sanitize_query() WP Tax Query::sanitize query WP 5.7.1
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;
}