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.