diff --git a/src/wp-includes/date.php b/src/wp-includes/date.php index 99bbb4e012..2f30fd53f2 100644 --- a/src/wp-includes/date.php +++ b/src/wp-includes/date.php @@ -256,16 +256,12 @@ class WP_Date_Query { if ( isset( $query['month'] ) && $value = $this->build_value( $compare, $query['month'] ) ) $where_parts[] = "MONTH( $column ) $compare $value"; - - // Legacy - if ( isset( $query['monthnum'] ) && $value = $this->build_value( $compare, $query['monthnum'] ) ) + else if ( isset( $query['monthnum'] ) && $value = $this->build_value( $compare, $query['monthnum'] ) ) $where_parts[] = "MONTH( $column ) $compare $value"; if ( isset( $query['week'] ) && false !== ( $value = $this->build_value( $compare, $query['week'] ) ) ) $where_parts[] = _wp_mysql_week( $column ) . " $compare $value"; - - // Legacy - if ( isset( $query['w'] ) && false !== ( $value = $this->build_value( $compare, $query['w'] ) ) ) + else if ( isset( $query['w'] ) && false !== ( $value = $this->build_value( $compare, $query['w'] ) ) ) $where_parts[] = _wp_mysql_week( $column ) . " $compare $value"; if ( isset( $query['dayofyear'] ) && $value = $this->build_value( $compare, $query['dayofyear'] ) ) diff --git a/tests/phpunit/tests/query/dateQuery.php b/tests/phpunit/tests/query/dateQuery.php index 47cc71d563..5e456e5c5d 100644 --- a/tests/phpunit/tests/query/dateQuery.php +++ b/tests/phpunit/tests/query/dateQuery.php @@ -555,4 +555,49 @@ class Tests_Query_DateQuery extends WP_UnitTestCase { $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) ); } + + public function test_date_params_monthnum_m_duplicate() { + $posts = $this->_get_query_result( array( + 'date_query' => array( + 'month' => 5, + 'monthnum' => 9 + ), + ) ); + + $expected_dates = array( + '1972-05-24 14:53:45', + '2003-05-27 22:45:07', + '2004-05-22 12:34:12', + '2007-05-16 17:32:22', + '2025-05-20 10:13:01', + ); + + $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) ); + + $this->assertContains( "AND ( ( MONTH( post_date ) = 5 ) ) AND", $this->q->request ); + + $this->assertNotContains( "AND ( ( MONTH( post_date ) = 5 AND MONTH( post_date ) = 9 ) ) AND", $this->q->request ); + } + + public function test_date_params_week_w_duplicate() { + $posts = $this->_get_query_result( array( + 'date_query' => array( + 'week' => 21, + 'w' => 22 + ), + ) ); + + $expected_dates = array( + '1972-05-24 14:53:45', + '2004-05-22 12:34:12', + '2025-05-20 10:13:01', + ); + + + $this->assertEquals( $expected_dates, wp_list_pluck( $posts, 'post_date' ) ); + + $this->assertContains( "AND ( ( WEEK( post_date, 1 ) = 21 ) ) AND", $this->q->request ); + + $this->assertNotContains( "AND ( ( WEEK( post_date, 1 ) = 21 AND WEEK( post_date, 1 ) = 22 ) ) AND", $this->q->request ); + } } \ No newline at end of file