Title: [Plugin: self build] Select posts with a set date
Last modified: August 20, 2016

---

# [Plugin: self build] Select posts with a set date

 *  Resolved [kramer65](https://wordpress.org/support/users/kramer65/)
 * (@kramer65)
 * [13 years, 6 months ago](https://wordpress.org/support/topic/plugin-self-build-select-posts-with-a-set-date/)
 * Hello,
 * I’ve just built my own plugin which basically displays posts on the frontpage
   which have a custom field called “featureddate” set to a date which is in the
   past. If the date is either a date in the future, or no featureddate is set at
   all, it should not display the post.
 * The code I have is as follows:
 *     ```
       <?
       function only_featured( $query ) {
           if ( $query->is_home() && $query->is_main_query() ) {
       	$query->set('meta_key', 'featureddate');
               $query->set('meta_compare', '<=');
               $query->set('meta_value', date('Ymd'));
           }
       }
       add_action( 'pre_get_posts', 'only_featured' );
       ?>
       ```
   
 * The problem I have now, is that it indeed only selects posts which have the featureddate
   in the past. But when I don’t set a date at all, it displays the post anyway.
 * Does anybody know how I can solve this so that when the featureddate is not set
   a at it doesn’t display the post on the front page? All tips are welcome!

Viewing 1 replies (of 1 total)

 *  Thread Starter [kramer65](https://wordpress.org/support/users/kramer65/)
 * (@kramer65)
 * [13 years, 6 months ago](https://wordpress.org/support/topic/plugin-self-build-select-posts-with-a-set-date/#post-3225818)
 * Turns out that when the date field is left empty it saves as a single space. 
   So I added the following:
 *     ```
       $query->set('meta_key', 'featureddate');
               $query->set('meta_compare', '!=');
               $query->set('meta_value', ' ');
       ```
   

Viewing 1 replies (of 1 total)

The topic ‘[Plugin: self build] Select posts with a set date’ is closed to new replies.

 * 1 reply
 * 1 participant
 * Last reply from: [kramer65](https://wordpress.org/support/users/kramer65/)
 * Last activity: [13 years, 6 months ago](https://wordpress.org/support/topic/plugin-self-build-select-posts-with-a-set-date/#post-3225818)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
