php - 在 AJAX 后过滤器中添加多个元键和值

标签 php jquery html ajax wordpress

我为自定义字段值创建了一个 AJAX 后过滤器。此过滤器呈现数据迭代 JSON 并使用自定义内容模板。这段代码非常适合自定义字段 brandvalues..

现在我面临在函数中添加多个元键的问题,之后我将能够过滤多个自定义字段值。

如何在函数 $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

<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>

表单.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>

最佳答案

是的,使用第三个参数 'compare' 可以很容易地做到这一点。

'meta_query' also contains one or more arrays with the following keys:

'key' (string) - Custom field key.

'value' (string|array) - Custom field value. It can be an array only when compare is 'IN', 'NOT IN', 'BETWEEN', or 'NOT BETWEEN'. You don't have to specify a value when using the 'EXISTS' or 'NOT EXISTS' comparisons in WordPress 3.9 and up.
(Note: Due to bug #23268, value is required for NOT EXISTS comparisons to work correctly prior to 3.9. You must supply some string for the value parameter. An empty string or NULL will NOT work. However, any other string will do the trick and will NOT show up in your SQL when using NOT EXISTS. Need inspiration? How about 'bug #23268'.)

'compare' (string) - Operator to test. Possible values are '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN', 'EXISTS' and 'NOT EXISTS'. Default value is '='.

'type' (string) - Custom field type. Possible values are 'NUMERIC', 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED'. Default value is 'CHAR'.


'relation' (string) - The logical relationship between each inner meta_query array when there is more than one. Possible values are 'AND', 'OR'. Do not use with a single inner meta_query array.

这是一个可视化示例,说明您可以使用 'relation' 参数做什么……

$args = array(
    'post_type' => 'post',
    'meta_query' => array(
        'relation' => 'OR',
        array(
            'key' => 'brand',
            'value' => $brand,
            'compare' => '=',
        ),
        array(
            'relation' => 'AND',
            array(
                'key' => 'color',
                'value' => 'red',
                'compare' => '=',
            ),
            array(
                'key' => 'size',
                'value' => 'small',
                'compare' => '=',
            ),
        ),
    ),
);

引用:Class_Reference WP_Query - Custom Field Parameters

关于php - 在 AJAX 后过滤器中添加多个元键和值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39038131/

相关文章:

php - 未定义的命名空间 html(将 css 文件添加到 blade)laravel

javascript - Google map Javascript API 的安全性

php - 在手动输入的两个日期之间搜索

php - 如何用包含客户端文件的受密码保护的目录在PHP中创建购物车

jquery - 如何增加 ASP.NET WebAPI Post 调用的 json 大小限制?

javascript - 如何通过搜索该元素的另一个属性来找到该元素的属性

PHP 获取别名特征的名称

javascript - 无法找到特定项目值 jQuery

jquery - 网格中心的一个 ui block - jquery mobile

java - HTTP Post 不工作?