WordPress.org

Ready to get started?Download WordPress

Forums

custom query_posts excluding a certain meta_key (6 posts)

  1. indranil
    Member
    Posted 3 years ago #

    Hello,

    I want to create a custom query, which only shows posts that do NOT have a certain meta_key. Is that possible? How?

    Thanks!

  2. keesiemeijer
    moderator
    Posted 3 years ago #

    I had the same problem over here: http://wordpress.org/support/topic/get_post-query-where-posts-dont-have-a-specific-meta_key-1 I don't think there is a clear solution for this problem so I made a sql query to do this.

  3. indranil
    Member
    Posted 3 years ago #

    Thanks, I guess I'll have to use that. :) Was trying to keep it simple :)

  4. keesiemeijer
    moderator
    Posted 3 years ago #

    You can also try this before the loop (not tested) :

    <?php
    $myposts = get_posts('numberposts=-1&meta_key=mykey');
    $result = array();
    foreach($myposts as $mypost) {
    setup_postdata($mypost);
    $result[] = $mypost->ID;
    }
    $args = array(
    'post__not_in' => $result
    );
    query_posts($args);
    ?>

    This gets all the post ID's with the specific meta_key (mykey) and then excludes them from the query.

  5. indranil
    Member
    Posted 3 years ago #

    That's intelligent, but I'm afraid it'll get rather server intensive with a large-ish blog :)

    Thanks for the help :)

  6. paulwillen
    Member
    Posted 3 years ago #

    @keesiemeijer That's a GREAT solution! Thanks for that! It worked right away!

Topic Closed

This topic has been closed to new replies.

About this Topic