0

I have a widget that gets posts from different post_type's. I want to exclude some posts that have a custom field value

e.g. I save a form where a checkbox adds a value of 0 or 1 depending if ticked. If the value is 0 the post should be visable and if the value is 1 then it should not be visable.

Here is what I have to extract the posts:

$wpcust = new WP_Query( 
array( 
'post_type' => array(
    'posts_type_1',
    'posts_type_2'
    ),   
'showposts' => '4' ) 
);

This is a basic WP_Query that selects posts based on two post_types.
What I want to add is:

'meta_key' => 'key',
'meta_value' => 'value'

Where value is 0 or 1. If I do this it only shows posts based on the meta_value. I need it the other way around where this excludes those posts.

Any help? Thnx

M.

0

The meta_query parameter in WP_Query allows you to query posts that don't have a certain custom field. Furthermore, you can check for multiple custom fields. With two queries in meta_query, you can fetch all posts that either don't have a custom field, or have it set to 0:

'meta_query' => array(
    'relation' => 'OR',
    array(
        'key' => 'my-key',
        'value' => '0'
    ),
    array(
        'key' => 'my-key',
        'compare' => 'NOT EXISTS',
        'value' => 'Use any value here' // If you're using WordPress 3.9+, you can omit "value" for NOT EXISTS
    )
)
  • Wow, That.was.awesome Thnx so much and so quick.... Nice. – Interactive Jun 20 '14 at 9:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.