Make WordPress Core

Opened 11 years ago

Closed 11 years ago

Last modified 11 years ago

#25835 closed defect (bug) (fixed)

WP_Date_Query duplicated Parameters

Reported by: chrico's profile ChriCo Owned by: wonderboymusic's profile 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 11 years ago.
git diff
25835.diff (1.2 KB) - added by ChriCo 11 years ago.
Diff
unit-test.25835.diff (2.3 KB) - added by oso96_2000 11 years ago.
Unit test and fixed diff route
unit-test.25835.2.diff (2.8 KB) - added by oso96_2000 11 years ago.
More tests

Download all attachments as: .zip

Change History (16)

#1 @toscho
11 years ago

  • Cc info@… added

#2 @SergeyBiryukov
11 years ago

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

Related: #25834

@ChriCo
11 years ago

git diff

#3 @ChriCo
11 years ago

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

#5 @emzo
11 years ago

  • Cc wordpress@… added

@ChriCo
11 years ago

Diff

#6 @wonderboymusic
11 years ago

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

@oso96_2000
11 years ago

Unit test and fixed diff route

#7 @oso96_2000
11 years ago

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

@oso96_2000
11 years ago

More tests

#8 @oso96_2000
11 years 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.

#9 @oso96_2000
11 years ago

  • Keywords dev-feedback added

#10 @nacin
11 years 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.

#11 @wonderboymusic
11 years 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.

#12 @wonderboymusic
11 years ago

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