LIKE %…% Meta Query

I came across this same issue today. In both our situations, there is in fact a nice solution. Because the serialized data we are searching for, are simply IDs, and they are always wrapped in quotations, we just need to include the quotations as part of the query. This eliminates the problem of encountering false positives in the search results.

You just need to modify the meta query to as follows:

array(
    'key' => 'practice_area',
    'value' => '"'.$post->ID.'"',
    'compare' => 'LIKE',
)

This way, it searches for the whole ID, including the double quotes before and after it within the serialized array. No false positives will be included.

Leave a Comment

error code: 523