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 building a real estate site where Estates is a custom post type. I want to be able to tick a checkbox or something similare on the edit page, where I can choose if the estate is for rent, if it contains office space etc. And then be able to query against this in different templates.

I´m wondering what´s the best way to achive this. I´m using 'Advanced Custom Fields' to type in a lot of information about the estates and that is working well. I thought I could just use a custom field checkbox for this aswell but it seems you can´t query against a custom field value if the values are in an array.

Should I be using taxonomies instead? I could do that, but it makes less sense for the user and I don´t want them to be able to add terms, which they can.

Any suggestions?

share|improve this question
    
Why are your custom field values saved as an array? Why don't you create separate custom field for every property "feature" - you will then be able to use meta_query when querying posts. –  Mateusz Hajdziony Jan 6 '13 at 2:03

1 Answer 1

You can certainly use the Advanced Fields plugin for this type of functionality - you'll just need to add a few lines to the query that creates your custom pages. I found the documentation on checkboxes from ACF to be helpful. Scroll through the code examples at the bottom of the page and you'll see this:

/*
 * Query posts for a checkbox value.
 * This method uses the meta_query LIKE to match the string "red" to 
 * the database value a:2:{i:0;s:3:"red";i:1;s:4:"blue";} (serialized array)
 * The above value suggests that the user selected "red" and "blue" from 
 * the checkbox choices
 */
 $posts = get_posts(array(
    'meta_query' => array(
        array(
            'key' => 'field_name', // name of custom field
            'value' => '"red"', // matches exaclty "red", 
                //not just red. This prevents a match for "acquired"
            'compare' => 'LIKE'
        )
    )
));

if( $posts )
{
    //...
}

You can modify this code to work with a WP_Query as well.

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.