From 8b0ef058f31029ee2cbc0df7d35654666465b310 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Tue, 1 Jul 2014 01:17:39 +0000 Subject: [PATCH] WP_Date_Query: The inclusive logic should include all times within the date range. props mboynes, oso96_2000, DrewAPicture. fixes #26653. git-svn-id: https://develop.svn.wordpress.org/trunk@28935 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/date.php | 13 ++++++++--- tests/phpunit/tests/query/dateQuery.php | 31 +++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/date.php b/src/wp-includes/date.php index e00452d7d1..0fbf0423e0 100644 --- a/src/wp-includes/date.php +++ b/src/wp-includes/date.php @@ -48,6 +48,9 @@ class WP_Date_Query { /** * Constructor. * + * @since 3.7.0 + * @since 4.0.0 The $inclusive logic was updated to include all times within the date range. + * * @param array $date_query { * One or more associative arrays of date query parameters. * @@ -235,19 +238,23 @@ class WP_Date_Query { $compare = $this->get_compare( $query ); + $inclusive = ! empty( $query['inclusive'] ); + + // Assign greater- and less-than values. $lt = '<'; $gt = '>'; - if ( ! empty( $query['inclusive'] ) ) { + + if ( $inclusive ) { $lt .= '='; $gt .= '='; } // Range queries if ( ! empty( $query['after'] ) ) - $where_parts[] = $wpdb->prepare( "$column $gt %s", $this->build_mysql_datetime( $query['after'], true ) ); + $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'], false ) ); + $where_parts[] = $wpdb->prepare( "$column $lt %s", $this->build_mysql_datetime( $query['before'], $inclusive ) ); // Specific value queries diff --git a/tests/phpunit/tests/query/dateQuery.php b/tests/phpunit/tests/query/dateQuery.php index 5e456e5c5d..058fde1670 100644 --- a/tests/phpunit/tests/query/dateQuery.php +++ b/tests/phpunit/tests/query/dateQuery.php @@ -254,6 +254,37 @@ class Tests_Query_DateQuery extends WP_UnitTestCase { $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) ); } + /** + * @ticket 26653 + */ + public function test_date_query_inclusive_between_dates() { + $posts = $this->_get_query_result( array( + 'date_query' => array( + 'after' => array( + 'year' => 2007, + 'month' => 1 + ), + 'before' => array( + 'year' => 2008, + 'month' => 12 + ), + 'inclusive' => true + ), + ) ); + + + $expected_dates = array( + '2007-01-22 03:49:21', + '2007-05-16 17:32:22', + '2007-09-24 07:17:23', + '2008-03-29 09:04:25', + '2008-07-15 11:32:26', + '2008-12-10 13:06:27', + ); + + $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) ); + } + public function test_date_query_year_expecting_results() { $posts = $this->_get_query_result( array( 'date_query' => array(