On this page
WP_Date_Query::get_sql_for_clause( array $query, array $parent_query ): string[]
Turns a first-order date query into SQL for a WHERE clause.
Parameters
$queryarray Required-
Date query clause.
$parent_queryarray Required-
Parent query of the current date query.
Return
string[] Array containing JOIN and WHERE SQL clauses to append to the main query.
joinstringSQL fragment to append to the main JOIN clause.wherestringSQL fragment to append to the main WHERE clause.
Source
File: wp-includes/class-wp-date-query.php. View all references
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(),
);
}
Related
Uses
| Uses | Description |
|---|---|
| esc_sql() wp-includes/formatting.php | Escapes data for use in a MySQL query. |
| WP_Date_Query::build_mysql_datetime() wp-includes/class-wp-date-query.php | Builds a MySQL format date/time based on some query parameters. |
| WP_Date_Query::build_value() wp-includes/class-wp-date-query.php | Builds and validates a value string based on the comparison operator. |
| WP_Date_Query::build_time_query() wp-includes/class-wp-date-query.php | Builds a query string for comparing time values (hour, minute, second). |
| WP_Date_Query::validate_column() wp-includes/class-wp-date-query.php | Validates a column name parameter. |
| WP_Date_Query::get_compare() wp-includes/class-wp-date-query.php | Determines and validates what comparison operator to use. |
| wpdb::prepare() wp-includes/class-wpdb.php | Prepares a SQL query for safe execution. |
Used By
| Used By | Description |
|---|---|
| WP_Date_Query::get_sql_for_query() wp-includes/class-wp-date-query.php | Generates SQL clauses for a single query array. |
| WP_Date_Query::get_sql_for_subquery() wp-includes/class-wp-date-query.php | Turns a single date clause into pieces for a WHERE clause. |
Changelog
| Version | Description |
|---|---|
| 4.1.0 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/classes/wp_date_query/get_sql_for_clause