admin管理员组文章数量:1278946
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.
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.
Share Improve this question edited Oct 13, 2021 at 22:35 dgl asked Oct 13, 2021 at 18:11 dgldgl 12 bronze badges 1 |1 Answer
Reset to default 0Figured 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.
本文标签: meta querymetaquery compare ““ returns nothing when it seems it should
版权声明:本文标题:meta query - meta_query compare “=“ returns nothing when it seems it should 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741264287a2368177.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
'value' => '20'
is very different from'value' => 20
; – jdm2112 Commented Oct 21, 2021 at 14:19