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 have a need to exclude posts based on a custom field type. It will probably be a field such as "legacy" or "depricated". I searched for an example on how this would be done but I haven't found anything worth while. Any help would be appreciated.

<?php
            $software = array('post_type' => 'software', 'category_name' => $model);
            query_posts( $software );
            get_template_part( 'loop', 'software' );
            wp_reset_query();
            ?>
share|improve this question

1 Answer 1

up vote 2 down vote accepted

I met the same problem before. I tried to use meta_query parameter in the query argument, but didn't succeed. The code I tried to use looks like this:

$software = array(
    'post_type' => 'software',
    'meta_query' => array('key' => 'legacy', 'value' => 1, 'compare' => '!=')
);
query_posts($software);

But the problem is this query also excludes posts that don't have that meta key.

So I had to use a custom query, that gets all posts with legacy meta key, and then excludes them:

global $wpdb;
$legacy = $wpdb->get_col("SELECT ID FROM $wpdb->postmeta WHERE meta_key='legacy'");

$software = array(
    'post_type' => 'software',
    'post__not_in' => $legacy
);
query_posts($software);

Hope this can help you.

share|improve this answer
    
Appreciate the help! I seem to be seeing the same posts. I'm confused how post__not_in works with custom fields? Does it understand when a key is on or off using a checkbox? –  Zach Shallbetter Jun 3 '11 at 16:05
    
The post__not_in only refers to an array of post IDs, which is $legacy in the example. The query that get $legacy only check if the meta key is set or not (but you can change it to check the meta value as well). –  Rilwis Jun 3 '11 at 17:04
    
Instead of custom query for IDs you can do usual get_posts() and add 'fields' => 'ids' to arguments. Highly useful recent addition, not known/documented much yet. :) –  Rarst Jun 3 '11 at 18:55
    
@Rarst: Nice point. I avoided to use get_posts() as I thought it would do a "big" query. Haven't known about the fields parameter. Thanks :) –  Rilwis Jun 4 '11 at 3:33

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.