WordPress at a glance
function is not described

WP_Date_Query::get_sql_for_clause() protected WP 4.1.0

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

{} It's a method of the class: WP_Date_Query{}

No Hooks.

Return

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

  • join(string)
    SQL fragment to append to the main JOIN clause.

  • where(string)
    SQL fragment to append to the main WHERE clause.

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.

Changelog

Since 4.1.0 Introduced.

Code of WP_Date_Query::get_sql_for_clause() WP 5.5.1

<?php
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(),
	);
}