Farooq Ahmad Farooq Ahmad - 3 months ago 16
jQuery Question

Add multiple meta key and value in AJAX post filter

I create a AJAX post filter for Custom Field Values. This filter render data iterating JSON and also use custom content template. And this code work perfectly for custom field brand and there values..

Now I'm facing problem to add multiple meta key in function after that I will be able to filter multiple custom fields value.

How to add multiple key and value in function

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


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>

Answer

Yes it's possible to do it easily, using the third argument '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.

Here it's a visual example of what you can do using 'relation' argument…

$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' => '=',
            ),
        ),
    ),
);

Reference: Class_Reference WP_Query - Custom Field Parameters

Comments