Make WordPress Core

Opened 13 years ago

Closed 13 years ago

#17264 closed defect (bug) (fixed)

Duplicate posts returned if multiple meta_values match and no meta_key is set

Reported by: trepmal's profile trepmal Owned by: dd32's profile dd32
Milestone: 3.1.3 Priority: normal
Severity: normal Version: 3.1
Component: Query Keywords: has-patch fixed-major
Focuses: Cc:

Description

I'm currently experiencing issues that appear to be related to the second point of 12660, "Using just meta_value returns duplicate posts based on the meta_value"

I have a post with multiple custom fields, for example

"address" with value "123 Lorem St"
"city" with value "Lorem"

When I perform a search to return posts with any meta_value LIKE 'sequim' ...

$args = array(
	'meta_query' => array(
		array(
			'value' => 'lorem',
			'compare' => 'LIKE'
		)
	)
);
query_posts($args);

... the post I described above is returned twice, once for each meta_value that matches.

I'd expect WP to handle the result in a way that duplicate posts would not be returned.

My current workaround is to require the user to choose a key to search in, but I'd like to be able to do more of an all-in-one search.

Attachments (5)

17264.diff (1.4 KB) - added by greuben 13 years ago.
patch to allow query without 'key' argument
17264.2.diff (1.2 KB) - added by greuben 13 years ago.
17264.3.diff (3.4 KB) - added by scribu 13 years ago.
unit-meta-query.php (4.5 KB) - added by scribu 13 years ago.
17264.4.diff (777 bytes) - added by greuben 13 years ago.
Patch for 3.1 branch

Download all attachments as: .zip

Change History (25)

#1 @scribu
13 years ago

  • Keywords reporter-feedback added

Please test this with the latest nightly build. You can use the Beta Tester plugin.

Version 0, edited 13 years ago by scribu (next)

#2 @trepmal
13 years ago

  • Keywords reporter-feedback removed

Point-release nightlies gets me 3.1.2-alpha
problem persists

Bleeding edge nightlies gets me 3.2-bleeding
produces the following error (and no search results):
WordPress database error: [You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 10' at line 1]
SELECT SQL_CALC_FOUND_ROWS wp_posts.* FROM wp_posts WHERE 1=1 AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private') AND ( ) GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 10

#3 @scribu
13 years ago

  • Milestone changed from Awaiting Review to 3.2

#4 @nacin
13 years ago

trepmal: Please try bleeding edge (3.2-bleeding, trunk).

#5 @trepmal
13 years ago

I was running 3.2-bleeding when I got the WordPress database error mentioned above.

I just double-checked my version number against what I got from http://core.svn.wordpress.org/trunk/

#6 @nacin
13 years ago

Ah sorry, I didn't see your full comment. All I saw was "Point-release nightlies". Thanks.

#7 @greuben
13 years ago

On trunk we cannot query without 'key' arg in meta_query(generates invalid sql)
Lines 495-496

if ( empty( $meta_key ) && empty( $meta_value ) )
	continue;

One small bug there is $meta_value is always empty there.

Regression introduced in [17699] and [17674]

@greuben
13 years ago

patch to allow query without 'key' argument

#8 @greuben
13 years ago

  • Keywords has-patch added

Forgot to mention, this is the invalid sql

AND ( ) GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 10 

@greuben
13 years ago

@scribu
13 years ago

#9 @scribu
13 years ago

17264.3.diff: handle empty and invalid meta queries.

unit-meta-query.php: more unit tests; also check JOIN clause.

#10 @dd32
13 years ago

(In [17746]) Properly handle querying for meta by value without a key as well as those with invalid parameters. Props scribu. See #17264

#11 @scribu
13 years ago

greuben deserved some percentage of props as well. :)

#12 @greuben
13 years ago

  • Keywords fixed-major added

@scribu: Thanks :)

@greuben
13 years ago

Patch for 3.1 branch

#13 @greuben
13 years ago

17264.4.diff fixes duplicate posts for 3.1 branch.

#14 follow-up: @nacin
13 years ago

Please only add the fixed-major keyword if you can also change the milestone. Otherwise it will get stuck in limbo.

Is this a regression in 3.1 from 3.0?

#15 in reply to: ↑ 14 @greuben
13 years ago

Replying to nacin:

Please only add the fixed-major keyword if you can also change the milestone. Otherwise it will get stuck in limbo.

Ok got it

Is this a regression in 3.1 from 3.0?

Yes

#16 @greuben
13 years ago

  • Keywords fixed-major removed

#17 follow-up: @dd32
13 years ago

  • Keywords fixed-major added
  • Milestone changed from 3.2 to 3.1.3

greuben deserved some percentage of props as well. :)

oops, Sorry Gruben, That's what testing for too long and committing it because you can no longer handle looking at code does to you.

I'll take a look at the 3.1 branch today

#18 in reply to: ↑ 17 @greuben
13 years ago

Replying to dd32:

greuben deserved some percentage of props as well. :)

oops, Sorry Gruben, That's what testing for too long and committing it because you can no longer handle looking at code does to you.

I understand its okay. BTW its Greuben

#19 @dd32
13 years ago

I understand its okay. BTW its Greuben

Seems I cant get anything right :P (sorry again)

#20 @dd32
13 years ago

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

In [17805]:

Do not return duplicate posts for Meta Queries. Props Greuben. Fixes #17264 for 3.1

Note: See TracTickets for help on using tickets.