Title: Exclude posts from wp_query based on meta_value
Last modified: August 20, 2016

---

# Exclude posts from wp_query based on meta_value

 *  Resolved [westleymon](https://wordpress.org/support/users/westleymon/)
 * (@westleymon)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/exclude-posts-from-wp_query-based-on-meta_value/)
 * Hi –
 * I’ve setup a custom post type for events with custom fields added to it (one 
   specifically being event date).
 * I’ve successfully queried the posts and sorted by the event date. My next goal
   is to exclude posts with an event date of at least yesterday.
 * Here is my code for the query:
 *     ```
       <?php
       $upcoming_events = new WP_Query(array('post_type' => 'event',
       		  'posts_per_page' => 4,
       		  'order_by' => 'meta_value',
       		  'meta_key' => '_refactord-datepicker',
       		  'order' => 'ASC'));
       while ( $upcoming_events->have_posts() ) : $upcoming_events->the_post(); ?>
       ```
   
 * The event date being called by `_refactord-datepicker`
 * Can someone please point me in the right direction for accomplishing my goal?
   The reason I’ve setup my custom event type rather than using a plugin is because
   the events need to have options turned on and off like Bottle Service Sign Up,
   Guest List, etc.
 * Thanks in advance!

Viewing 1 replies (of 1 total)

 *  Thread Starter [westleymon](https://wordpress.org/support/users/westleymon/)
 * (@westleymon)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/exclude-posts-from-wp_query-based-on-meta_value/#post-2364830)
 * Found the answer to my question on this [post](http://wordpress.org/support/topic/get_posts-sorting-using-meta_compare-by-event-date?replies=4).
 * Changed my code to a get_posts function which enabled me to use the meta_compare
   option in the query.
 * For anyone who might be wondering, my new code looks like this:
 *     ```
       $postslist = get_posts('post_type=event&numberposts=4&order=ASC&meta_key=event_date&meta_compare=>=&meta_value=' . date("m/d/Y") . '&orderby=meta_value_num');
        foreach ($postslist as $post) :
        setup_postdata($post);
       // post layout
       endforeach;
       ```
   
 * Enjoy!

Viewing 1 replies (of 1 total)

The topic ‘Exclude posts from wp_query based on meta_value’ is closed to new replies.

## Tags

 * [events](https://wordpress.org/support/topic-tag/events/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 1 reply
 * 1 participant
 * Last reply from: [westleymon](https://wordpress.org/support/users/westleymon/)
 * Last activity: [14 years, 7 months ago](https://wordpress.org/support/topic/exclude-posts-from-wp_query-based-on-meta_value/#post-2364830)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
