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'm having trouble with the code below. Attempting to do a query against meta data stored with the posts, in the mls post type. I think I know what the issue is, but not how to fix it. I am guessing it has something to do with the value being stored as a string. I've searched and found other people confused about this, but not a solution. Here are two examples I've attempted with no luck.

<?php $args = array(
        'post_type' => 'mls',
        'meta_query' => array(
            relation => 'AND', 
            array(
                'key' => '_city',
                'value' => 'Clarkson',
                'compare' => '='
            ),
            array(
                'key' => '_price',
                'value' => '200000',                    
                'type' => 'numeric',
                'compare' => '>='
            )
            //array(
            //  'key' => '_price',
            //  'value' => array( 200000, 1000000 ),
            //  'type' => 'numeric',
            //  'compare' => 'BETWEEN'
            //)
        )
    ); ?>
share|improve this question
1  
There is nothing technically wrong with the above code snippet. Those are valid arguments for WP_Query() - you either have to share more of the queries code, or your values are simply not represented in the database. –  Johannes Pille Oct 18 '12 at 7:17
    
@JohannesPille - You are absolutely correct. After beating my head against the wall. Your comment sent me to look again to verify. I installed the debug admin plugin. Along with the add-on that shows meta data. Some of the data was normalized and renamed. There was indeed a _city field. However, the _price field was actually named __price... and there was a __city that I should have been using ( notice they have two under scores, and that was my issue. THANKS!!! ) –  Scott Hack Oct 18 '12 at 7:46

2 Answers 2

Try using the Docs here. I tried the same thing and it worked for me.

share|improve this answer
    
That is what I was using. What is different about my example above and the example code in the docs? –  Scott Hack Oct 18 '12 at 6:09
    
are you sure about the operators you are using in the query work properly? Try the given example in the documentation as per your requirement. –  Rohit Pande Oct 18 '12 at 6:15

Maybe I'm wrong but you are missing query relation...

<?php $args = array(
        'post_type' => 'mls',
        'meta_query' => array(
            relation => 'AND', 
            array(
                'key' => '_city',
                'value' => 'Clarkson',
                'compare' => '='
            ),
            array(
                'key' => '_price',
                'value' => '200000',                    
                'type' => 'numeric',
                'compare' => '>='
            )
            //array(
            //  'key' => '_price',
            //  'value' => array( 200000, 1000000 ),
            //  'type' => 'numeric',
            //  'compare' => 'BETWEEN'
            //)
        )
    ); ?>
share|improve this answer
    
You are right. I've since tested that as well, but still no joy. However, I can tell you that by changing the relation to "OR" I was able to get it to return posts. Which does narrow the issue down to something the key _price portion of the query. –  Scott Hack Oct 18 '12 at 7:00
    
Try with "OR" and compare => 'BETWEEN' for a case key value is not strictly set to 200000. –  Dameer Oct 18 '12 at 7:04
    
Trying with OR and the BETWEEN example above only returns posts that match the _city of Clarkson. Still nothing returning based on the _price key. –  Scott Hack Oct 18 '12 at 7:13
    
AND is the default relation - there is no need to set it explicitly (see /wp-includes/meta.php, line 647). –  Johannes Pille Oct 18 '12 at 7:22

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.