I develope a filter for posts so that when any user come to my site they can filter the posts on the base of cities/state
.
So when i store the posts i just store the cities name as a post-meta
tag in the site. The code i am using for adding the post-meta
tag is
add_post_meta($post_id, 'post_state' , $state, true);
The html which generating the names of the cities in the form of checkbox is here
for(x in state_arr){
jQuery('#state_div').append('<label><input type="checkbox" name="stateName['+ state_arr[x] +']" id="" value="'+ state_arr[x] +'" >' + state_arr[x] + '</label');
}
So i pass the name as array
.
And the code i am using for filter is here
<?php
if ( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) && $_POST['action'] == 'sortbydestination' ) :
$stateName = $_POST['stateName'];
foreach($stateName as $key => $stN)
{
$citynames = $stN . ",";
}
unset($_POST);
endif;
?>
And the argument which i pass to the query is look like this
$args = array(
'posts_per_page' => 30,
'paged' => $paged,
'post_type' => 'post',
'post_status' => 'publish',
'meta_query' => array(
'key' => 'post_state',
'value' => array($citynames )
)
);
I want to know one thing. Here i in query the value
need one value but i have to pass multiple valuse to compare and find the result so how can i do that.
unset($_POST);
. So i just simply remove it and my code is working now.