0

I am trying to do something like this:

$args = array(
'post_type' => 'product',
'meta_query' => array(
    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?

2

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

var_dump($query->request);

And you can see that for yourself.

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.