Title: WP_Date_Query::sanitize_query
Published: December 18, 2014
Last modified: April 28, 2025

---

# WP_Date_Query::sanitize_query( array $queries, array $parent_query = null ): array

## In this article

 * [Description](https://developer.wordpress.org/reference/classes/wp_date_query/sanitize_query/?output_format=md#description)
 * [Parameters](https://developer.wordpress.org/reference/classes/wp_date_query/sanitize_query/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/classes/wp_date_query/sanitize_query/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/classes/wp_date_query/sanitize_query/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/classes/wp_date_query/sanitize_query/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/classes/wp_date_query/sanitize_query/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/classes/wp_date_query/sanitize_query/?output_format=md#wp--skip-link--target)

Recursive-friendly query sanitizer.

## 󠀁[Description](https://developer.wordpress.org/reference/classes/wp_date_query/sanitize_query/?output_format=md#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](https://developer.wordpress.org/reference/classes/wp_date_query/sanitize_query/?output_format=md#parameters)󠁿

 `$queries`arrayrequired

`$parent_query`arrayoptional

Default:`null`

## 󠀁[Return](https://developer.wordpress.org/reference/classes/wp_date_query/sanitize_query/?output_format=md#return)󠁿

 array Sanitized queries.

## 󠀁[Source](https://developer.wordpress.org/reference/classes/wp_date_query/sanitize_query/?output_format=md#source)󠁿

    ```php
    public function sanitize_query( $queries, $parent_query = null ) {
    	$cleaned_query = array();

    	$defaults = array(
    		'column'   => 'post_date',
    		'compare'  => '=',
    		'relation' => 'AND',
    	);

    	// Numeric keys should always have array values.
    	foreach ( $queries as $qkey => $qvalue ) {
    		if ( is_numeric( $qkey ) && ! is_array( $qvalue ) ) {
    			unset( $queries[ $qkey ] );
    		}
    	}

    	// Each query should have a value for each default key. Inherit from the parent when possible.
    	foreach ( $defaults as $dkey => $dvalue ) {
    		if ( isset( $queries[ $dkey ] ) ) {
    			continue;
    		}

    		if ( isset( $parent_query[ $dkey ] ) ) {
    			$queries[ $dkey ] = $parent_query[ $dkey ];
    		} else {
    			$queries[ $dkey ] = $dvalue;
    		}
    	}

    	// Validate the dates passed in the query.
    	if ( $this->is_first_order_clause( $queries ) ) {
    		$this->validate_date_values( $queries );
    	}

    	// Sanitize the relation parameter.
    	$queries['relation'] = $this->sanitize_relation( $queries['relation'] );

    	foreach ( $queries as $key => $q ) {
    		if ( ! is_array( $q ) || in_array( $key, $this->time_keys, true ) ) {
    			// This is a first-order query. Trust the values and sanitize when building SQL.
    			$cleaned_query[ $key ] = $q;
    		} else {
    			// Any array without a time key is another query, so we recurse.
    			$cleaned_query[] = $this->sanitize_query( $q, $queries );
    		}
    	}

    	return $cleaned_query;
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/class-wp-date-query.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-includes/class-wp-date-query.php#L189)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/class-wp-date-query.php#L189-L237)

## 󠀁[Related](https://developer.wordpress.org/reference/classes/wp_date_query/sanitize_query/?output_format=md#related)󠁿

| Uses | Description | 
| [WP_Date_Query::sanitize_relation()](https://developer.wordpress.org/reference/classes/wp_date_query/sanitize_relation/)`wp-includes/class-wp-date-query.php` |

Sanitizes a ‘relation’ operator.

  | 
| [WP_Date_Query::is_first_order_clause()](https://developer.wordpress.org/reference/classes/wp_date_query/is_first_order_clause/)`wp-includes/class-wp-date-query.php` |

Determines whether this is a first-order clause.

  | 
| [WP_Date_Query::validate_date_values()](https://developer.wordpress.org/reference/classes/wp_date_query/validate_date_values/)`wp-includes/class-wp-date-query.php` |

Validates the given date_query values and triggers errors if something is not valid.

  | 
| [WP_Date_Query::sanitize_query()](https://developer.wordpress.org/reference/classes/wp_date_query/sanitize_query/)`wp-includes/class-wp-date-query.php` |

Recursive-friendly query sanitizer.

  |

| Used by | Description | 
| [WP_Date_Query::sanitize_query()](https://developer.wordpress.org/reference/classes/wp_date_query/sanitize_query/)`wp-includes/class-wp-date-query.php` |

Recursive-friendly query sanitizer.

  | 
| [WP_Date_Query::__construct()](https://developer.wordpress.org/reference/classes/wp_date_query/__construct/)`wp-includes/class-wp-date-query.php` |

Constructor.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/classes/wp_date_query/sanitize_query/?output_format=md#changelog)󠁿

| Version | Description | 
| [4.1.0](https://developer.wordpress.org/reference/since/4.1.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fclasses%2Fwp_date_query%2Fsanitize_query%2F)
before being able to contribute a note or feedback.