WordPress.org

Make WordPress Core

Opened 3 years ago

Closed 3 years ago

#17264 closed defect (bug) (fixed)

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

Reported by: trepmal Owned by: 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 3 years ago.
patch to allow query without 'key' argument
17264.2.diff (1.2 KB) - added by greuben 3 years ago.
17264.3.diff (3.4 KB) - added by scribu 3 years ago.
unit-meta-query.php (4.5 KB) - added by scribu 3 years ago.
17264.4.diff (777 bytes) - added by greuben 3 years ago.
Patch for 3.1 branch

Download all attachments as: .zip

Change History (25)

comment:1 scribu3 years ago

  • Keywords reporter-feedback added

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

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

comment:2 trepmal3 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

comment:3 scribu3 years ago

  • Milestone changed from Awaiting Review to 3.2

comment:4 nacin3 years ago

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

comment:5 trepmal3 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/

comment:6 nacin3 years ago

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

comment:7 greuben3 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]

greuben3 years ago

patch to allow query without 'key' argument

comment:8 greuben3 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 

greuben3 years ago

scribu3 years ago

scribu3 years ago

comment:9 scribu3 years ago

17264.3.diff: handle empty and invalid meta queries.

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

comment:10 dd323 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

comment:11 scribu3 years ago

greuben deserved some percentage of props as well. :)

comment:12 greuben3 years ago

  • Keywords fixed-major added

@scribu: Thanks :)

greuben3 years ago

Patch for 3.1 branch

comment:13 greuben3 years ago

17264.4.diff fixes duplicate posts for 3.1 branch.

comment:14 follow-up: nacin3 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?

comment:15 in reply to: ↑ 14 greuben3 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

comment:16 greuben3 years ago

  • Keywords fixed-major removed

comment:17 follow-up: dd323 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

comment:18 in reply to: ↑ 17 greuben3 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

comment:19 dd323 years ago

I understand its okay. BTW its Greuben

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

comment:20 dd323 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.