WP_Date_Query::get_sql_for_clause()protectedWP 4.1.0

Turns a first-order date query into SQL for a WHERE clause.

Method of the class: WP_Date_Query{}

No Hooks.

Return

Array. Array containing JOIN and WHERE SQL clauses to append to the main query.

Usage

// protected - for code of main (parent) or child class
$result = $this->get_sql_for_clause( $query, $parent_query );
$query(array) (required)
Date query clause.
$parent_query(array) (required)
Parent query of the current date query.

Notes

  • Global. wpdb. $wpdb WordPress database abstraction object.

Changelog

Since 4.1.0 Introduced.

WP_Date_Query::get_sql_for_clause() code WP 6.5.2

protected function get_sql_for_clause( $query, $parent_query ) {
	global $wpdb;

	// The sub-parts of a $where part.
	$where_parts = array();

	$column = ( ! empty( $query['column'] ) ) ? esc_sql( $query['column'] ) : $this->column;

	$column = $this->validate_column( $column );

	$compare = $this->get_compare( $query );

	$inclusive = ! empty( $query['inclusive'] );

	// Assign greater- and less-than values.
	$lt = '<';
	$gt = '>';

	if ( $inclusive ) {
		$lt .= '=';
		$gt .= '=';
	}

	// Range queries.
	if ( ! empty( $query['after'] ) ) {
		$where_parts[] = $wpdb->prepare( "$column $gt %s", $this->build_mysql_datetime( $query['after'], ! $inclusive ) );
	}
	if ( ! empty( $query['before'] ) ) {
		$where_parts[] = $wpdb->prepare( "$column $lt %s", $this->build_mysql_datetime( $query['before'], $inclusive ) );
	}
	// Specific value queries.

	$date_units = array(
		'YEAR'           => array( 'year' ),
		'MONTH'          => array( 'month', 'monthnum' ),
		'_wp_mysql_week' => array( 'week', 'w' ),
		'DAYOFYEAR'      => array( 'dayofyear' ),
		'DAYOFMONTH'     => array( 'day' ),
		'DAYOFWEEK'      => array( 'dayofweek' ),
		'WEEKDAY'        => array( 'dayofweek_iso' ),
	);

	// Check of the possible date units and add them to the query.
	foreach ( $date_units as $sql_part => $query_parts ) {
		foreach ( $query_parts as $query_part ) {
			if ( isset( $query[ $query_part ] ) ) {
				$value = $this->build_value( $compare, $query[ $query_part ] );
				if ( $value ) {
					switch ( $sql_part ) {
						case '_wp_mysql_week':
							$where_parts[] = _wp_mysql_week( $column ) . " $compare $value";
							break;
						case 'WEEKDAY':
							$where_parts[] = "$sql_part( $column ) + 1 $compare $value";
							break;
						default:
							$where_parts[] = "$sql_part( $column ) $compare $value";
					}

					break;
				}
			}
		}
	}

	if ( isset( $query['hour'] ) || isset( $query['minute'] ) || isset( $query['second'] ) ) {
		// Avoid notices.
		foreach ( array( 'hour', 'minute', 'second' ) as $unit ) {
			if ( ! isset( $query[ $unit ] ) ) {
				$query[ $unit ] = null;
			}
		}

		$time_query = $this->build_time_query( $column, $compare, $query['hour'], $query['minute'], $query['second'] );
		if ( $time_query ) {
			$where_parts[] = $time_query;
		}
	}

	/*
	 * Return an array of 'join' and 'where' for compatibility
	 * with other query classes.
	 */
	return array(
		'where' => $where_parts,
		'join'  => array(),
	);
}