WordPress.org

Make WordPress Core

Opened 3 years ago

Last modified 41 hours ago

#18707 new enhancement

Meta Query printing "array" in query when compare type is LIKE

Reported by: tplayer Owned by:
Milestone: Future Release Priority: normal
Severity: normal Version: 3.2.1
Component: Query Keywords: has-patch needs-testing meta-query needs-refresh
Focuses: Cc:

Description

Always a try to filter post by meta_query using a array value and compare equals to "LIKE" the returned SQL query is buged. WordPress return something like this:

(wp_postmeta.meta_key = 'passed_key_value' AND CAST(wp_postmeta.meta_value AS CHAR) LIKE '%Array%'))

The array values are not serialized, and "Array" are outputed to in SQL Query.

Serializing the values the problem still the same and WordPress output in query somethinhg like this

(wp_postmeta.meta_key = 'passed_key_value' AND CAST(wp_postmeta.meta_value AS CHAR) LIKE '%Array[0], Array[1], Array[2]%'))

According the WP_Query documentation, array is supported in field "value" for meta_query filters.

Attachments (2)

multiplelikes.18707.diff (1.7 KB) - added by robmiller 17 months ago.
First go at allowing multiple LIKE values in a meta query
18707.diff (1.6 KB) - added by wonderboymusic 9 months ago.

Download all attachments as: .zip

Change History (8)

comment:1 dd323 years ago

  • Type changed from defect (bug) to enhancement

According the WP_Query documentation, array is supported in field "value" for meta_query filters.

The codex just isn't very explicit in the accepted values. A string is expected in all cases, except for IN and BETWEEN (and their NOT counterparts) which both accept an array (or comma separated list of items).

Marking this as an enhancement as such that it's not broken, just not implemented :)

robmiller17 months ago

First go at allowing multiple LIKE values in a meta query

comment:2 robmiller17 months ago

  • Cc r@… added
  • Keywords has-patch 2nd-opinion added; needs-patch removed

Here's a first, rough crack at extending the meta query logic to allow an array of values. If a single value is passed, you get the LIKE "%foo%" that you'd expect; if you pass an array of values, you get e.g. LIKE "%foo%" OR LIKE "%bar%".

Would appreciate anyone's thoughts; it feels somewhat dirty at the moment but that's primarily because I didn't want to redo the current logic too much.

Usage: new WP_Query(array('meta_key' => 'foo', 'meta_value' => array('foo', 'bar'), 'meta_compare' => 'like'));

Last edited 17 months ago by robmiller (previous) (diff)

wonderboymusic9 months ago

comment:3 wonderboymusic9 months ago

  • Keywords needs-testing added; 2nd-opinion removed

Patch needs testing and I shudder to think how badly multiple wildcard searches will perform with meta query. Not ready to move this over.

comment:4 wonderboymusic6 months ago

  • Keywords meta-query added

comment:5 wonderboymusic2 days ago

  • Keywords needs-refresh added
  • Milestone changed from Awaiting Review to Future Release

This does make some sense from an API perspective - my performance concerns are still vast

comment:6 SergeyBiryukov41 hours ago

  • Summary changed from Meta Query printing "array" in query whwn compara type is LIKE to Meta Query printing "array" in query when compare type is LIKE
Note: See TracTickets for help on using tickets.