WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

            $software = array('post_type' => 'software', 'category_name' => $model);
            query_posts( $software );
            get_template_part( 'loop', 'software' );
share|improve this question
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' => '!=')

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

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


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.