我有一个搜索页面,用户的搜索结果按邮政编码或县名显示。在搜索页面上,我有一个复选框,可以按属性过滤用户,如开发者、设计师和帐户,因此访问者可以按属性过滤邮政编码的用户。所有字段均由高级自定义字段创建。我正在通过ajax显示用户数据。但不知道问题出在哪里。请帮忙
这是要显示复选框字段的表单。
<div class="sectionContent filter_list">
<?php
acf_form([
\'field_groups\' => [\'50\'],
\'fields\' => [\'category\',
\'type_of_session\',
\'wheelchair_access\',
\'telephone_life_coaching\',
\'face_to_face_life_coaching\',
\'home_visits\',
\'sign_language\',
],
\'form\' => false
])
?>
</div>
<script>
jQuery(function($){
$(\'.filter_list input\').on(\'change\', function()
{
var url = \'<?php echo admin_url( \'admin-ajax.php\' ); ?>?action=find_coach\';
$(\'.filter_list input\').each(function()
{
var thisVal, metaKey;
if($(this).prop(\'checked\') == true)
{
thisVal = $(this).val();
//may be the problem in metakey
metaKey = getParamFromUrl(\'pin_code\');
url += \'&acf[\'+metaKey+\'][]=\'+thisVal;
}
});
$.get(url, function(res){
$.each(res, function(key, val){
$user_row = \'<div data-template="member" class="member">\\
<div class="user">\\
<div class="article_left">\\
<a href="\'+val.user_url+\'"><img src="\'+val.avatar+\'"></a>\\
</div>\\
<div class="article_right">\\
<div class="profile-display-address">\\
<h2 class="member-name">\\
<a href="\'+val.user_url+\'">\'+val.display_name+\'</a>\\
</h2>\\
<div class="col-6">\'+val.address+\'</div>\\
<div class="col-6">\'+val.address_2+\'</div>\\
<div class="col-6">\'+val.pin_code+\'</div>\\
</div>\\
<div class="contact-form contact-links"><a href="#">Contact me</a></div>\\
</div>\\
<div class="button-profile"><a class="button whitebg" href="\'+val.user_url+\'">View profile</a></div>\\
</div>\';
document.getElementById(\'member_list\').innerHTML = $user_row;
});
});
});
$(\'.filter_list input\').each(function(){
//checkbox handling
if($(this).prop(\'tagName\') == \'INPUT\' && $(this).attr(\'type\') == \'checkbox\'){
var val = getParamFromUrl($(this).attr(\'name\'))
if(val.indexOf($(this).val()) > -1){
$(this).prop(\'checked\', true);
}
}
});
});
function getParamFromUrl(name){
var url = new URL(window.location.href);
var c = url.searchParams.getAll(name);
return c;
}
</script>
add_action(\'wp_ajax_search_coach\', \'searchLifeCoach\');
add_action(\'wp_ajax_nopriv_search_coach\', \'searchLifeCoach\');
function searchLifeCoach(){
//print_R($_GET);
$metaData = [];
$fields = acf_get_fields($field_group_key);
if(isset($_GET[\'acf\']) && !empty($_GET[\'acf\'])){
foreach($_GET[\'acf\'] as $key => $field){
$metaData[] = [
\'key\' => $key,
\'value\' => $field,
\'compare\' => is_array($field) ? \'LIKE\' : \'=\'
];
}
}
$args = array (
\'order\' => \'ASC\',
\'orderby\' => \'display_name\',
\'role__not_in\' => \'administrator\',
//\'fields\' => \'all_with_meta\',
\'meta_query\' => array(
\'relation\' => \'OR\',
array(
\'key\' => \'pincode\',
\'value\' => sanitize_text_field($_GET[\'city\']),
\'compare\' => \'=\'
),
array(
\'key\' => \'city\',
\'value\' => sanitize_text_field($_GET[\'city\']),
\'compare\' => \'=\'
),
),
\'meta_query\' => array(
\'relation\' => \'AND\',
$metaData
)
);
// Create the WP_User_Query object
$wp_user_query = new WP_User_Query( $args );
//echo \'<pre>\'; print_r($wp_user_query); echo \'</pre>\';
// Get the results
$authors = $wp_user_query->get_results();
foreach($authors as $author){
$userMeta = get_user_meta($author->data->ID);
//echo \'<pre>\'; print_r($userMeta); echo \'</pre>\';
$userList[] = [
\'uid\' => $author->data->ID,
\'display_name\' => $author->data->display_name,
\'email\' => $author->data->user_email,
\'user_url\' => $author->data->user_url,
\'avatar\' => (isset($userMeta[\'avatar\']) ? wp_get_attachment_url($userMeta[\'avatar\'][0]) : \'\'),
\'first_name\' => (isset($userMeta[\'first_name\']) ? $userMeta[\'first_name\'][0] : \'\'),
\'last_name\' => (isset($userMeta[\'last_name\']) ? $userMeta[\'last_name\'][0] : \'\'),
\'user_phone\' => (isset($userMeta[\'phone\']) ? $userMeta[\'phone\'][0] : \'\'),
\'address\' => (isset($userMeta[\'address\']) ? $userMeta[\'address\'][0] : \'\'),
\'address_2\' => (isset($userMeta[\'address_2\']) ? $userMeta[\'address_2\'][0] : \'\'),
\'pin_code\' => (isset($userMeta[\'pin_code\']) ? $userMeta[\'pin_code\'][0] : \'\'),
\'age\' => (isset($userMeta[\'age\']) ? $userMeta[\'age\'][0] : \'\'),
\'bio\' => (isset($userMeta[\'coach_bio\']) ? $userMeta[\'coach_bio\'][0] : \'\'),
\'city\' => (isset($userMeta[\'city\']) ? $userMeta[\'city\'][0] : \'\'),
\'city_2\' => (isset($userMeta[\'city_2\']) ? $userMeta[\'city_2\'][0] : \'\'),
\'zip_code_2\' => (isset($userMeta[\'zip_code_2\']) ? $userMeta[\'zip_code_2\'][0] : \'\'),
\'zip_code_2\' => (isset($userMeta[\'zip_code_2\']) ? $userMeta[\'zip_code_2\'][0] : \'\'),
];
}
return wp_send_json($userList);
exit();
}
最合适的回答,由SO网友:Tom J Nowell 整理而成
Th类e problem级 我s your 一ct型我on n一m级es don\'t型 m级一t型ch类
个A.;个A.;
我n your front型end code:
个A.;个A.;
?一ct型我on=f我nd_co一ch类\';个A.;
个A.;个A.;
But型 我n your b一ckend code:
个A.;个A.;
一dd_一ct型我on(\'wp_一j一x个_se一rch类_co一ch类\', \'se一rch类L我feCo一ch类\');个A.; 一dd_一ct型我on(\'wp_一j一x个_nopr我v_se一rch类_co一ch类\', \'se一rch类L我feCo一ch类\');个A.;
个A.;个A.;
se一rch类_co一ch类
!= f我nd_co一ch类
, t型h类ey h类一ve t型o m级一t型ch类.
个A.;个A.;
Not型e t型h类一t型 我f t型h类我s code used t型h类e REST A.P我, 我t型 would h类一ve g级我ven 一 4.04. 一nd st型一t型ed t型h类e problem级 我n h类um级一n re一d一ble l一ng级u一g级e, e.g级.
个A.;个A.;
{个A.; “”code“”:“”rest型_no_rout型e“”,个A.; “”m级ess一g级e“”:“”No rout型e w一s found m级一t型ch类我ng级 t型h类e URL 一nd request型 m级et型h类od“”,个A.; “”d一t型一“”:{“”st型一t型us“”:4.04.}个A.;}个A.;
个A.;