WordPress.org

Make WordPress Core

Opened 16 months ago

Closed 10 months ago

Last modified 10 months ago

#25835 closed defect (bug) (fixed)

WP_Date_Query duplicated Parameters

Reported by: ChriCo Owned by: wonderboymusic
Milestone: 4.0 Priority: low
Severity: normal Version: 3.7
Component: Query Keywords: has-patch
Focuses: Cc:

Description

In WP_Date_Query-Class there a 2 duplicated Parameters:

  • month <-> monthnum
  • week <-> w

When creating an Query, we can set both of them:

$date_query = new WP_Date_Query( array(
  'w' => 12,
  'week' => 11
));
echo $date_query->get_sql();
/*
AND ( 
  ( WEEK( post_date, 1 ) = 11 
  AND WEEK( post_date, 1 ) = 12 ) 
)
*/

and:

$date_query = new WP_Date_Query( array(
'month' => 12,
'monthnum' => 11
));
echo $date_query->get_sql();
/*
AND ( 
  ( MONTH( post_date ) = 12 
  AND MONTH( post_date ) = 11 ) 
)
*/

In date.php should be an else if to avoid the duplicated AND-Search with no result at all:

if ( isset( $query['week'] ) && false !== ( $value = $this->build_value( $compare, $query['week'] ) ) )
	$where_parts[] = _wp_mysql_week( $column ) . " $compare $value";
else if ( isset( $query['w'] ) && false !== ( $value = $this->build_value( $compare, $query['w'] ) ) )
	$where_parts[] = _wp_mysql_week( $column ) . " $compare $value";

and..

if ( isset( $query['month'] ) && $value = $this->build_value( $compare, $query['month'] ) )
	$where_parts[] = "MONTH( $column ) $compare $value";
else if ( isset( $query['monthnum'] ) && $value = $this->build_value( $compare, $query['monthnum'] ) )
	$where_parts[] = "MONTH( $column ) $compare $value";

Attachments (4)

date.php.rej (1.6 KB) - added by ChriCo 16 months ago.
git diff
25835.diff (1.2 KB) - added by ChriCo 15 months ago.
Diff
unit-test.25835.diff (2.3 KB) - added by oso96_2000 13 months ago.
Unit test and fixed diff route
unit-test.25835.2.diff (2.8 KB) - added by oso96_2000 13 months ago.
More tests

Download all attachments as: .zip

Change History (16)

comment:1 @toscho16 months ago

  • Cc info@… added

comment:2 @SergeyBiryukov16 months ago

  • Component changed from Date/Time to Query
  • Version changed from 3.7.1 to 3.7

Related: #25834

@ChriCo16 months ago

git diff

comment:3 @ChriCo16 months ago

I've added a .diff-File ( pls ignore the file-extension ) to fix this problem.

comment:5 @emzo15 months ago

  • Cc wordpress@… added

@ChriCo15 months ago

Diff

comment:6 @wonderboymusic14 months ago

  • Keywords has-patch needs-unit-tests added
  • Milestone changed from Awaiting Review to 3.9

@oso96_200013 months ago

Unit test and fixed diff route

comment:7 @oso96_200013 months ago

Hope the unit test works. Also uploaded the diff with the whole change since the path to the file has changed.

@oso96_200013 months ago

More tests

comment:8 @oso96_200013 months ago

  • Keywords needs-unit-tests removed

Wasn't happy with the tests uploaded before so I added one more assert to each test to check for returned posts.

comment:9 @oso96_200013 months ago

  • Keywords dev-feedback added

comment:10 @nacin11 months ago

  • Milestone changed from 3.9 to Future Release
  • Priority changed from normal to low

This isn't a huge deal, moving to Future, we can pick it up in 4.0.

comment:11 @wonderboymusic10 months ago

  • Owner set to wonderboymusic
  • Resolution set to fixed
  • Status changed from new to closed

In 28252:

In WP_Date_Query::get_sql_for_subquery(), don't parse duplicate parameters - only parse one of w and week or month and monthnum.

Adds unit tests.

Props oso96_2000, ChriCo.
Fixes #25835.

comment:12 @wonderboymusic10 months ago

  • Keywords dev-feedback removed
  • Milestone changed from Future Release to 4.0
Note: See TracTickets for help on using tickets.