meta query – meta_query compare “=“ returns nothing when it seems it should

Question

Trying to query based on form submission. One select on the form is built with:

wp_dropdown_categories( 'name=specialty&taxonomy=specialties&orderby=name&show_option_none=Select a Specialty&show_none_value=-1&selected='.$p_specialty );

The specialty value is then grabbed and an array created:

if (isset($_GET['specialty']) && !empty($_GET['specialty']) && $_GET['specialty'] > 0 ) {
    $specialty = true;
    ++$qs_count;
    $p_specialty = $_GET['specialty'];
    $p_specialty_array = array (
        'key' => 'specialties',
        'value' => $p_specialty,
        'compare' => 'LIKE',
    );
    $term_specialty = get_term($p_specialty);
    $term_specialty = $term_specialty->name; 
}

$p_specialty_array is then used in:

$meta_query = array (
        'relation'      => 'AND',
        array (
            'q_last'        => array (
                'key'       => 'provider_name_last_name',
            ),
            'q_first'       => array (
                'key'       => 'provider_name_first_name',
            ),
            'compare'       => 'IN',
            $p_fname_array,
            $p_lname_array,
            $p_new_array,
            $p_gender_array,
            $p_clinic_array,
            $p_specialty_array,
        ),
    );

But this causes a problem because the ID of one specialty is 20 and another is 204. When the form is submitted for specialty=20 using compare=LIKE it returns posts where specialty is either 20 or 204 (rather than just 20).

When I change the compare to “=“, nothing is returned even though there are posts where specialty=20.

I’ve also tried adding value_field=‘slug’ to the wp_dropdown_categories, but can’t figure out how to make that work (it may work and I just don’t know how to write the array when using the slug).

I would certainly prefer a method where it only gets “=“ values rather than “LIKE” to avoid issues like this, but can’t get it to work.

How do I make this work with compare “=”, or something else, where I get only the specialty selected?

0
user9124823 1 week 2021-10-13T13:11:27-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse