0
votes

I have an events page displaying an 'events' custom post type. At the top of the page I have 2 upcoming posts in an upcoming event list and then at the bottom I have 3 past events in a past events list. Posts are displayed in each section based on if an 'event date' custom field is in the past or not. The past events uses WP page navi plugin to paginate the posts. 'posts_per_page' is set at one as a test.

There should be 3 pages that have 1 past post displayed on them, respectively. Unfortunately, the pagination shows 5 pages, the first two have no posts displayed on them, as if the posts from the upcoming posts lists are behaving as ghosts. I have removed the upcoming posts list as a test but this makes no difference.

Does anyone know any reason for this? I have no idea. Thanks.

<?php
    $temp_post = $post; // Store the Page the Post

    // Get the current date
    $current_date = date('M d, Y', time());
    $current_date = strtotime( $current_date );

            $wp_query= null;
            $wp_query = new WP_Query();
            $args = array( 'post_type' => 'events','posts_per_page' => 1,'paged' => $paged,'meta_key' => 'event_date', 'orderby' => 'meta_value_num', 'order' => 'DESC');
            $wp_query->query( $args );

            if ( $wp_query->have_posts() ) : 
            while ( $wp_query->have_posts() ) : $wp_query->the_post(); 

               // Get the date custom field
                $post_date = get_field('event_date');
                $post_date = strtotime( $post_date );

                // If older than post date, don't show it
                if(  $current_date > $post_date ): ?>

        MY POST CONTENT GOES HERE

        <?php endif; // END DATE FILTER ?>
        <?php endwhile; //END LOOP ?>

        <?php /* Start wp-pagenavi support */ ?>
        <?php if(function_exists('wp_pagenavi') ) : 
            echo '<nav class="pag">'; ?>
            <?php wp_pagenavi(); ?>
            <?php echo '</nav>'; ?>
        <?php else: ?>  
            <?php twentyeleven_content_nav( 'nav-below' ); ?>
        <?php endif; ?><?php /* End wp-pagenavi support */ ?>

    <?php endif; ?>
    <?php $post = $temp_post; // Reset the Post ?>
2

2 Answers

0
votes

It's acting as ghost because you are not showing it. You are showing the navigation in the bottom of page. The query returns 5 page, so navigation would be for 5 page.

But you aren't showing the post, if it doesn't meet this condition

// If older than post date, don't show it
if(  $current_date > $post_date ): ?>

That's why those posts are not showing. Remove that condition and you will see that all the posts are showing as it should.

If you want to display pagination this way, you would have to limit the number of returned post from query itself.

0
votes

Thanks a lot, I managed to work it out with a push in the right direction.

    // Get the current date
    $current_date = date('Ymd', time());


            $wp_query= null;
            $wp_query = new WP_Query();
            $args = array( 
                            'post_type' => 'events',
                            'posts_per_page' => 1,
                            'paged' => $paged, 
                            'orderby' => 'meta_value_num', 
                            'order' => 'DESC',
                            'meta_key' => 'event_date', 
                            'meta_query' => array(
                                                array(
                                                        'key' => 'event_date',
                                                        'value' => $current_date,
                                                        'compare' => '<',
                                                        'type' => 'CHAR'
                                                    )
                                        )
                            );

            $wp_query->query( $args );

            if ( $wp_query->have_posts() ) : 
            while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>

            MY POST CONTENT HERE


        <?php //endif; // END DATE FILTER ?>
        <?php endwhile; //END LOOP ?>

        <?php /* Start wp-pagenavi support */ ?>
        <?php if(function_exists('wp_pagenavi') ) : 
            echo '<nav class="pag">'; ?>
            <?php wp_pagenavi(); ?>
            <?php echo '</nav>'; ?>
        <?php endif; ?><?php /* End wp-pagenavi support */ ?>

    <?php endif; ?>