我爲自定義字段值創建了AJAX後期過濾器。此篩選器呈現數據迭代JSON並使用自定義內容模板。而對於自定義字段品牌此代碼的工作非常有值 ..在AJAX後期過濾器中添加多個元鍵和值
現在我面臨的問題之後添加多個meta鍵的功能,我將能夠過濾多個自定義字段值。
如何在函數$args
中添加多個鍵和值?
Function.php
add_action('wp_ajax_call_post', 'call_post');
add_action('wp_ajax_nopriv_call_post', 'call_post');
function call_post(){
$params = wp_parse_args ($_REQUEST, array(
));
$brand = $params['mobile'];
$args = array(
'post_type' => 'post',
'meta_query' => array(
array(
'key' => 'brand',
'value' => $brand,
) ,
) ,
);
$query = new WP_Query($args);
if(! empty ($params['template'])) {
$template = $params['template'];
if($query->have_posts()) :
while($query->have_posts()): $query->the_post();
get_template_part('content');
endwhile;
wp_reset_query();
else :
wp_send_json($query->posts);
endif;
die();
}
}
腳本
<script>
jQuery(document).ready(function() {
jQuery('.br').click(function() {
jQuery('.contents').remove();
var checked = jQuery('#test').serialize();
$('.filter-output').empty()
jQuery.ajax({
url: "<?php echo admin_url('admin-ajax.php'); ?>",
data: "action=call_post&template=content&" + checked,
success: function (result) {
jQuery(result).appendTo('.filter-output');
}
});
})
});
</script>
form.php的
<form id='test' >
<input type="checkbox" name="mobile[]" value="Nokia" class="br"> NOKIA
<input type="checkbox" name="mobile[]" value="LG" class="br"> LG
<div class="filter-output">
</div>
</form>
如何使用 '$品牌= $ PARAMS [' 移動'];'對於每個密鑰 – FRQ6692
您可以使用像'key'=> $ variable'這樣的變量替換''key''這樣的任何參數,並且可以替換''value''。這是有效的,我已經在使用它......但「關係」和「比較」參數對於獲得你想要的東西很重要。這就像一個SQL查詢... – LoicTheAztec