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 display a custom post type (properties) and exclude posts that have a certain meta value (sold). Is there a way that I can do this? I have this for my code so far:

                        $args = array(
                            'post_type'      => 'property',
                            'orderby'        => 'meta_value',
                            'meta_key'       => 'random_775',
                            'order'          => 'ASC',
                            'posts_per_page' => 100,
                        );

                    $the_query = new WP_Query( $args ); ?>
                    <?php if ( $the_query->have_posts() ) : ?>
                    <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

So right now it shows all the custom post types even the ones that are 'sold'. How can I show the ones that are not sold? Also, sold is just a checkbox so it would have to know whether it was checked or not.

Suggestions? Thanks!

share|improve this question

1 Answer 1

up vote 2 down vote accepted

If you don't use any other meta data, just add the following to your $args:

'meta_key' => 'sold',
'meta_value' => true, // or whatever it is you're using here
'meta_compare' => '!=',

Otherwise, use a WP_Meta_Query:

'meta_query' => array(
    array(
        'key' => 'sold',
        'value' => true,  // or whatever it is you're using here
        'compare' => 'NOT LIKE',
    ),
),

See here for more information:

share|improve this answer
    
Thanks, that worked but I no longer have the posts in alphabetical order. Meta_key -> 'random_775' was what was doing it but now that I have two meta_keys, it broke. Any suggestion on how to keep the order? –  graphical_force Sep 26 '13 at 23:29
    
Hm, that's why I wrote the second part using a WP_Meta_Query. So this doesn't work either? Keep the meta_key and orderby parameters from your original code, and add the meta_query. If this is not working, I will post another solution (which uses standard sorting of PHP). –  t f Sep 27 '13 at 7:07
    
Thanks for the help, but I have another question where I'm using pre_get_posts but I still am having issues. link –  graphical_force Sep 27 '13 at 14:20

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.