I am trying to do something like this:

$args = array(
'post_type' => 'product',
'meta_query' => array(
        'key' => 'custom-text-field',
        'value' => '%rocket%',
        'compare' => 'LIKE'
$query = new WP_Query( $args );

My goal being that the query will search through all the text of the custom-text-field and return any post where rocket is found. However it isn't working (I either get all the posts or no results). Is this possible directly in the WP_Query or do I need to use $wpdb to construct something first?


WP_Query will add the % wildcard operators for a LIKE query. Just remove those. As is your query looks like LIKE '%\\%rocket\\%% which is not what you want. Try


And you can see that for yourself.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.