WordPress.org

Make WordPress Core

Opened 4 years ago

Closed 4 years ago

#18401 closed defect (bug) (duplicate)

Passing array as 'value' to 'meta_query' yields failed SQL request

Reported by: Doggie52 Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.2.1
Component: General Keywords:
Focuses: Cc:

Description

Say I have a custom post type called 'review' and it has a custom field called 'score'. The one and only 'review' has a 'score' of 3. The following code will output a $posts object with that 'review'.

$args = array('post_type' => 'review');
$args['meta_query'] = array(
	array(
	'key' => 'score',
	'value' => 3
));
$posts = new WP_Query($args);

SQL request made:

SELECT SQL_CALC_FOUND_ROWS  wp_posts.* FROM wp_posts  INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id) WHERE 1=1  AND wp_posts.post_type = 'review' AND (wp_posts.post_status = 'publish' OR wp_posts.post_author = 1 AND wp_posts.post_status = 'private') AND ( (wp_postmeta.meta_key = 'score' AND CAST(wp_postmeta.meta_value AS CHAR) = '3') ) GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 10

When passing an array with that '4', however, it does something out of the ordinary. The $posts object does not contain a post, and look at the SQL request around wp_postmeta.meta_value.

$args = array('post_type' => 'review');
$args['meta_query'] = array(
	array(
	'key' => 'score',
	'value' => array(3)
));

SQL request:

SELECT SQL_CALC_FOUND_ROWS  wp_posts.* FROM wp_posts  INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id) WHERE 1=1  AND wp_posts.post_type = 'review' AND (wp_posts.post_status = 'publish' OR wp_posts.post_author = 1 AND wp_posts.post_status = 'private') AND ( (wp_postmeta.meta_key = 'score' AND CAST(wp_postmeta.meta_value AS CHAR) = '') ) GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 10

I apologize if the Component marked is incorrect, I was unable to find something that fit good enough.

Attachments (2)

18401.patch (539 bytes) - added by Doggie52 4 years ago.
If array is passed and 'compare' isn't already set to IN, NOT IN, BETWEEN, it will default to IN
18401-fixed.patch (540 bytes) - added by Doggie52 4 years ago.
Fixed version of the above. Line numbers were wrong.

Download all attachments as: .zip

Change History (7)

comment:1 @Doggie524 years ago

  • Summary changed from meta_query's value fails with arrays to Passing array as 'value' to 'meta_query' yields failed SQL request

comment:2 @Doggie524 years ago

Forgot to mention the following error that appears when using the array(3):
Warning: trim() expects parameter 1 to be string, array given in xxxx\wp-includes\meta.php on line 526

comment:3 @johnbillion4 years ago

IIRC if you specify an array for the value parameter, you need to specify the compare parameter too (eg. IN, NOT IN, BETWEEN) else it defaults to '='.

Maybe the compare parameter should default to IN if an array is passed as the value parameter.

comment:4 @Doggie524 years ago

Ah.

I can confirm that setting 'compare' to 'IN' works, but I agree that there should be either documentation or a built-in fix to mitigate this.

@Doggie524 years ago

If array is passed and 'compare' isn't already set to IN, NOT IN, BETWEEN, it will default to IN

@Doggie524 years ago

Fixed version of the above. Line numbers were wrong.

comment:5 @scribu4 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Status changed from new to closed
Note: See TracTickets for help on using tickets.