0

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?

Update

I've tried the following just for kicks and all show empty results:

if (isset($_GET['specialty']) && !empty($_GET['specialty']) && $_GET['specialty'] > 0 ) {
    $specialty = true;
    ++$qs_count;
    $p_specialty = $_GET['specialty'];
    $p_spec_minus = $p_specialty - 1;
    $p_specialty_plus = $p_specialty + 1;
    echo 'plus: '.$p_specialty_plus;
    $p_specialty_array = array (
        'key' => 'specialties',
        'value' => array( $p_spec_minus, $p_specialty_plus),
        'compare' => 'BETWEEN',
    );
    $term_specialty = get_term($p_specialty);
    $term_specialty = $term_specialty->name;
}

In this next one I tried hardcoding the 20, but same empty results:

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

I'm very confused as to why this isn't working.

1
  • Is the data stored as an integer or a string? In PHP, 'value' => '20' is very different from 'value' => 20;
    – jdm2112
    Commented Oct 21, 2021 at 14:19

1 Answer 1

0

Figured it out from a comment made here.

The fix was to change this:

'value' => $p_specialty,

... to this:

'value' => '"'.$p_specialty.'"',

... and leave the LIKE compare as-is.

Just for more info, I had also tried serializing that value but that didn't work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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