W3cubDocs

/WordPress

WP_Date_Query::get_sql_for_clause( array $query, array $parent_query )

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

Parameters

$query

(array) (Required) Date query clause.

$parent_query

(array) (Required) Parent query of the current date query.

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.

Source

File: wp-includes/class-wp-date-query.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(),
		);
	}

Changelog

Version Description
4.1.0 Introduced.

© 2003–2019 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/classes/wp_date_query/get_sql_for_clause