WordPress.org

Ready to get started?Download WordPress

Forums

Can I Exclude Posts with Custom Field Names/Values? (2 posts)

  1. johnasp
    Member
    Posted 5 years ago #

    I've created a custom field called 'Snippet' (and have created three different values, each post can belong to one of these three values) which I've applied to certain posts/news items (just the small news items on my site). I've got all these snippets to appear in my side bar using the excellent 'Query Posts' plugin.

    I don't want any of these posts to appear in the main loop however and I'm struggling to work out how to do this. Can you filter out posts by using custom fields? I tried the below (in bold) but it didn't work:

    $the_query = new WP_Query('meta_key=-Snippet&cat=-'. $ex_feat . ',-' . $ex_vid . ',-' . $ex_aside . '&showposts=' . $showposts . '&orderby=post_date&order=desc');

    Am I going about this the correct way?

  2. stvwlf
    Member
    Posted 5 years ago #

    One way to do it, if you are willing to do the extra work up front, would be to create a post category called snippet. When you assign a snippet to a post, also assign it to the snippet category. wp_query/query_posts easily excludes a category from a query.

    I see you have this available also

    Custom Field Parameters
    Retrieve posts (or Pages) based on a custom field key or value.
        * meta_compare= - operator to test the meta_value=, default is '=', with other possible values of '!=', '>', '>=', '<', or '<='

    you could try it out and see if a test like meta_compare<=' ' would accomplish what you want - instead of trying to exclude, that would be what to include.

    http://codex.wordpress.org/Template_Tags/query_posts#Custom_Field_Parameters

Topic Closed

This topic has been closed to new replies.

About this Topic