• Resolved kramer65

    (@kramer65)


    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

    (@kramer65)

    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.