Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
    
The problem in this code is after code i unset the post unset($_POST);. So i just simply remove it and my code is working now. –  Adi Mar 28 '13 at 12:18

2 Answers 2

I think the issue is on the meta_query you need to add an other argument like

'compare' => '='

you can also use relation.

I suggest you try the query first.

share|improve this answer
    
Nops this is not an issue. By default it compare both key and values. –  Adi Mar 28 '13 at 6:28
    
why not use ajax? –  Poxtron Mar 28 '13 at 6:47
    
I have no idea how to use it. The main concern here is. Wehter i am passing the value in correct way or not. –  Adi Mar 28 '13 at 6:58

Meta query expects nested arrays, even if you only have one query. You will have to pass nested array to meta_query as following :

$args = array(
        'posts_per_page' => 30,
        'paged' => $paged, 
        'post_type' => 'post', 
        'post_status' => 'publish',
        'meta_query' => array(  array(
                        'key' => 'post_state',
                        'value' => array($citynames )
                              )
                        ) 
        );

For more information see this page.

share|improve this answer
    
tell me one thing. $citynames have multiple values here. so this meta_query work for multiple values or not. because it still showing me all results. Don't no why. Any solution for that –  Adi Mar 28 '13 at 7:16
    
I find the problem finally. Thanks all of you who help me. –  Adi Mar 28 '13 at 7:21
    
@Adi Now either post your own solution here and mark it as answer two days later, or check if any of those answers were the solution and in case this is true, mark them as solution. Thanks. –  kaiser Mar 28 '13 at 8:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.