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 am using my custom filter. So in this search there are many checkboxes (have city name) and user selected some of them the post show the search result base on the checkbox. First i sotre the city/state name by using the add_post_meta

add_post_meta($post_id, 'post_state' , $state, true);

So after that when user filter the post. i am using the code

if ( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) && $_POST['action'] == 'sortbydestination' ) :
$stateName = $_POST['stateName'];

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

                            ) 
            );

The checkbox html is generated by jquery which is

jQuery('#state_div').append('<label><input type="checkbox" name="stateName['+ state_arr[x] +']" id="" value="'+ state_arr[x] +'" >' + state_arr[x] + '</label');

So the name of the input is a array it self, which give me values like this

stateName[cityname1];
stateName[cityname2];
stateName[cityname3];
stateName[cityname4];

and so on. So someone tell me wether i am using right way to get these data or make correct query or not. Thanks

share|improve this question
    
You comparison operator is =, yet you feed the metaquery an array as a value. That cannot work. Simply pass $stateName as a value. –  Johannes Pille Mar 27 '13 at 12:34
    
I did that to. I remove the 'compare' => '=' but it not working. It show me all the posts. so what can i do now –  Adi Mar 27 '13 at 12:42
add comment

1 Answer

Thats because you got the meta_query wrong. Check documentation in Codex

Your query should be:

$args = array(
            'posts_per_page' => 30,
            'paged' => $paged, 
            'post_type' => 'post', 
            'post_status' => 'publish',
            'meta_query' => array(
                                array(
                                 'key' => 'post_state',
                                 'value' => $stateName,
                                 'compare' => '=' // not necesary by default is "="
                                )
                            ) 
            );

Basically you need to pass inside the meta_query array, another array with the key and value. Also $stateName should be a var.

share|improve this answer
    
i just remove the 'compare' => '=', But after that its still not working. It show me the all posts. –  Adi Mar 27 '13 at 12:47
    
did you copy the code exactly as mine? leave compare there, or remove previous "," –  chifliiiii Mar 27 '13 at 13:16
    
Yes i am using the same code as you suggested me. There is one problem i think, i am getting the data by post using this $stateName = $_POST['stateName']; in html stateName[] is array. so i am using right way to get it or not. –  Adi Mar 27 '13 at 13:20
    
You should edit your question and post the whole code. HTML included. I can´t help you out with that little info –  chifliiiii Mar 27 '13 at 13:23
    
check my question now i upadate it –  Adi Mar 27 '13 at 13:48
add comment

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.