Meta_query 'compare' => 'LIKE' not working?

RobbTe asked
custom-field query-posts meta-query
via

I have meta field with key”app_i_have” and values in my mysql db like: a:3:{i:0;s:14:"mobile studio";i:1;s:12:"own studio";i:2;s:9:"makeup artist";} Now i want to query posts through the pre_get_posts hook like this:

add_action( 'pre_get_posts', 'rt_tax_archive' );
function rt_tax_archive($query) {
    if (is_admin()) {
        return; /* If we're in the admin panel - drop out */
    }
    global $wp_query;
//For searching
    if ($query->is_main_query() && isset($_GET['ls'])) {
    $rt_field_id_i_have = $_GET['listing_i_have'];
        //Filter posts by what they have
        if (isset($rt_field_id_i_have) && 
        !empty($rt_field_id_i_have[0])) {
            $meta_query[] = array(
                    'key'=>'app_i_have',
                    'value'=>$rt_field_id_i_have[0],
                    'compare'=>'LIKE',
                );
        }
$query->set('meta_query', $meta_query);
}
}

This works, but when i want to filter on people with, for instance, “own studio” the 'compare'=>'LIKE' is also showing people with a “mobile studio”. I guess because the word “studio” is in there.

How can i correctly filter the posts in this situation? Thanks


Answer
via

If the query string contains, as you say:
listing_i_have=own%20studio

It’s not working because $rt_field_id_i_have[0] is equal to the first character in the string (it’s not an array), it’s a string. The 0 index position is the letter o, which matches A LOT of things.

You can add the proper meta query like so.

<?php
function rt_tax_archive($query) {
    if (is_admin()) {
        return;
    } elseif ( ! $query->is_main_query() ) {
        return;
    } elseif ( ! isset( $_GET['ls'] ) ) {
       return;
    } elseif ( empty( $_GET['listing_i_have'] ) ) {
       return;
    } elseif ( ! is_string( $_GET['listing_i_have'] ) ) {
       return;
    }
    $rt_field_id_i_have = wp_unslash( $_GET['listing_i_have'] );
    $rt_field_id_i_have = sanitize_text_field( $rt_field_id_i_have );

    $query->set('meta_query', array(
        'key'    => 'app_i_have',
        'value'  => $rt_field_id_i_have,
        'compare'=> 'LIKE',
    ));
}
add_action( 'pre_get_posts', 'rt_tax_archive' );

It’s worth noting that when using LIKE, WordPress automatically wraps the value inside %[value]%, so you don’t need to include the LIKE wildcards yourself. You weren’t doing so, just thought I’d mention that for posterity.

Share This
Posted in: