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 trying to show all rental properties, first by all properties that have not been rented, and then by all properties that are currently rented. There is a custom post type 'rent' with custom post meta for price rented (_price_rented) which is a checkbox (returns either true or false... true if it HAS been rented). I need to change the query to show all properties with the available (non-rented) properties appearing first and then the rented properties appearing.

Here is my query:

$ts_properties = new WP_Query( 
    array( 
    'post_type' => 'rent', 
    'paged' => $paged, 
    'posts_per_page' => -1,
    'meta_key' => '_price_rented',
    'orderby' => 'meta_value',
    'order' => 'DESC',
    'meta_query' => array(
        array(
        'key' => '_price_rented',
        'value' => false,
        'type' => 'BOOLEAN',
        ),
    ) 
) 
);

For some reason this query shows all properties that HAVE been rented. When I switch the value from 'false' to 'true' in the meta_query it doesn't show any properties.

So, then I thought, the return value is either false (for properties that ARE rented) or NULL (for properties that are NOT rented), but am not sure how to query for a NULL result (not false), I added a 'compare' argument to the meta_query and set the value to '!=' but that didn't work either.

EDIT: var_dump returns the following for an available, non-rented apartment: string(0) "" and for a non available, rented, apartment: string(1) "1"

share|improve this question
    
using values 1 and 0 perhaps? –  reikyoushin Jul 31 '13 at 15:24
    
meta_query type => string . Possible values are 'NUMERIC', 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED'. Default value is 'CHAR'. –  iEmanuele Jul 31 '13 at 15:29
    
@reikyoushin: using a '1' returns all rented properties, and a '0' returns no properties. –  Kegan Quimby Jul 31 '13 at 15:29
1  
@iEmanuele: changing that seems to have no effect (I thought the same thing). I saw that from this article: thethemefoundry.com/blog/… –  Kegan Quimby Jul 31 '13 at 15:30
1  
Is _price_rented actually set for both true and false values, or is it only set for true? Check the database please. I asked because an unchecked check box is not passed through POST at all so I am wondering if the value is set at all for those cases. –  s_ha_dum Jul 31 '13 at 15:45

2 Answers 2

WP_Meta_Query is a somehow "not so stable" part in core and if you don't pay verrry much attention it can easily break from being confused.

When you're doing a new WP_Query() and have meta_query => array() arguments or its single key/value pair equivalents, then new WP_Meta_Query() jumps in, instantly followed by parsing.

$this->meta_query = new WP_Meta_Query();
$this->meta_query->parse_query_vars( $q );

Allowed values

When you query meta data, then there's bool option. And if you'd use it, then it would fall back to CHAR, which the default value as the array of allowed values is:

'NUMERIC', 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED'

where NUMERIC will be reset to SIGNED.

Debugging

There're numerous filters that can affect the post save process, so the first thing to do is checking the different values inside some loop:

var_dump( get_post_meta( get_the_ID(), '_price_rented', true ) );

Then, depending on the return value, you'll either have to use SIGNED, if the result is 0 or 1, or "true" or "false" if the result is a string. If it really is boolean, then I'd still suggest to use string just to make sure it passes $GLOBALS['wpdb'], which can only pass %s string and %d digit through.

Additional Notes

As I just updated the Codex entry for WP_Meta_Query today, I saw that there're lots of different outputs (adding numerous amounts of unneeded JOINS, which are discussed on trac here and here without a single patch moved into core) possible. Point is that it is possible to use a combination of meta_* arguments alongside the meta_query array and its subarrays. The result is pretty much unknown unless you dump it, so IMHO you're better off using either the one or the other way of adding inputs. Especially when you're only using meta_key, as this results in a "key only query" in some cases.

Solution

As pointed out in the comments:

(...) var_dump returns the following for an available, non-rented apartment: string(0) "" and for a non available, rented, apartment: string(1) "1"

Now the meta_query has to use

'meta_query' => array( 'relation' => 'OR', array(
    'meta_key'   => '_price_rented',
    'meta_value' => '1',
    'compare'    => '='
) );

If you want to get the "non available, rented apartments" or use '!=' to retrieve the "non rented" apartments.

share|improve this answer

I faced the same problem and after an hour of search found the "NOT EXISTS" and "EXISTS" value ( only in WP >= 3.5 ). So no need to ask for a meta value just check if the meta_key exist :

'meta_key'     =>   '_price_rented'  ,
'meta_compare' =>   'NOT EXISTS'     ,

It's working perfectly for me.

share|improve this answer

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.