我为自定义字段值创建了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