meta_query compare “=“ returns nothing when it seems it should

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?

meta_query order by date present -> future then show null

Thanks for taking the time to read this. I’ve been struggling with a meta_query for an events site I’m working on.

I have used ACF to create a field for date start and date end, but not all events will have a date.

What I’m trying to achieve, is when you go to the archive or tax view, the first thing you see are the posts that have a date assigned, in order from today’s date into the future. Then after those dated events have been output, to cycle through all empty date posts.

So far I have the below in my functions.php file. This kind of works, but in the wrong order. So the correct events that are dated are output and in the right order. But only after the null valued items have output. I thought that may be because of the ordering in the arrays themselves, so moved the date ordered array to the end. That had no effect.

$  query->set( 'post_type', 'courses' ); $  query->set( 'meta_query', array(     'relation' => 'OR',     array(         'key'        => '_course_date_from',         'compare'    => '=',         'value'      => '',     ),     array(         'key'     => '_course_date_from',         'compare' => '>=',         'value'   => date('Ymd'),     ) ) ); $  query->set( 'orderby', 'meta_value title' ); $  query->set( 'order', 'ASC' ); 

Thanks for your time. Ben.

Meta_query on same meta key, with diffrenct values

i’m trying to get_users with the meta_query where i need to find users where either they don’t have the meta key, or it is blank.

It seems like everywhere i look, i should be able to use the OR on the relation and then use the same meta key, but how ever i set it up, it does not work.

My code:

$  ors[] = array(    'key'   => 'death_date',    'compare' => '=',    'value' => '',    'type' => 'NUMERIC'                     ); $  ors[] = array(    'key'   => 'death_date',    'compare' => 'NOT EXISTS'                     );  $  args = array(    'meta_query' => array(    'relation' => 'or',    $  ors, ));  $  return = get_users($  args); 

Both of them work separately, but not together.