Ignore posts from before today PHP

I have the following loop in WordPress and even though it always worked (at least, I think so), it recently stopped working as it should:

<?php
    $items = 0;
    $thiscat = get_category(3);
    $cat_slug = $thiscat->slug;
    $args = array( 
    'post_type' => 'Course', 
    'posts_per_page' => 3,
    'meta_key' => 'date_start',
    'orderby' => 'meta_value',
    'category_name' => $cat_slug,
    'order' => 'ASC',
); ?>
<ul>
    <?php
        $loop = new WP_Query( $args );
        while ( $loop->have_posts() AND $items < 3) {
        $loop->the_post();              
        $category_course = get_the_category(3);                 
        global $post;
        $category_detail = get_the_category( $post->ID );
        $date_start = get_post_meta(get_the_ID(), 'date_start', true);
        $place = get_post_meta(get_the_ID(), "place", true);
        if( $date_start >= strtotime('today') ) { ?>
        <li>                            
            <a href="<?php the_permalink(); ?>" class="date_fp"><?php echo strftime("%a, %e. %b. %Y", $date_start); ?> - <?php echo $place;?></a>
                <?php foreach ($category_detail as $category_ID)
                    {
                        if($category_ID->cat_name == 'z_aflyst')
                        {
                            echo "- <strong>Aflyst</strong>";
                        }
                }?>
            </li>
            <?php $items++; }
            }
            if($items==0) { ?>
                <li>                            
                    Ingen kommende kurser
                </li>
    <?php } ?>

The expected result: Count all courses that are to be held in the future, display a maximum of 3 on the front page

Read More

The outcome: Count all courses that are in the database (both past and present) with a maximum of 3, display the ones to be held on the front page (the ones held in the past are not displayed, but are counted). If 3 or more courses held in the past, it doesn’t display any of the courses to be held in the future.

In my head, it should be ignoring all posts with a date from before today, but it apparently still counts them, as the output on the front page is only one course (in the case of the above loop, where there are two courses held in the past and three planned for the future), instead of the available 3. I found out that if I change the posts_per_page to 4, it’ll display one more course, so it really has to do with the fact that it also counts the courses in the past as a post in the posts_per_page.

It’s probably just a small tweak of my code, but how and where do I make sure it ignores posts from before strotime('today')?

Related posts

Leave a Reply

1 comment

  1. By adapting my $args‘s meta-query, I managed to compare the value of the meta-key with a value of my choosing (in my case time()).

    By adding this, it was even possible to remove my if-sentence where I would find out if a course was placed in the future or past.

    $args['meta_query'] = array(
        array(
            'key' => 'date_start',
            'value' => time(),
            'compare' => '>='
            )
        )
    

    This results in the $args variable only grabbing posts that are equal or greater than time(). Epic solution thanks to StackOverflow, as it even made my code more readable.