Support

Account

Home Forums General Issues List posts by date picker with pagination Reply To: List posts by date picker with pagination

  • @hube2 I appreciate any help you can provide and totally understand your position regarding your framework and all the work that has gone into it.

    I have an “event” custom post type with the date picker field included. I’m using the following code in a page template to pull in the current month’s events, which is working fine. But I have no clue how to get this to paginate from month to month. What is needed is for the user to arrive on the page showing the current month’s events, then be able to click through to the next month’s events, and so on, including clicking back to the previous month but not past the current month. My guess is that I need to use WP_Query instead of get_posts, but still not sure how to get that to work.

    Again, any help you can provide is greatly appreciated!

    <?php
            $year = date('Y');
            $month = date('m');
            $posts = get_posts(array(
                'post_type' => 'event',
                'posts_per_page' => -1,
                'order' => 'ASC',
                'orderby' => 'meta_value',
                'meta_key' => 'date',
                'meta_query' => array(
                    'relation' => 'AND',
                    array(
                        'key' => 'date',
                        'value' => $year.''.$month.'01',
                        'compare' => '>=',
                    ),
                    array(
                        'key' => 'date',
                        'value' => $year.''.$month.'31',
                        'compare' => '<=',
                    )       
                )
            ));
            if( $posts ) : foreach( $posts as $p ) :
            $date_string = get_field('date', $p->ID);
            $date = DateTime::createFromFormat('Ymd', $date_string);         
            endforeach;
            ?>
            <div class="small-12 columns text-center">
                <h2><?php echo $date->format('F Y'); ?></h2>
            </div>
            <?php
            foreach( $posts as $p ) :
            $background_color = get_field('background_color', $p->ID);
                if($background_color == 'terracotta') {
                    $bkg = '#AD7A67';
                } elseif($background_color == 'sand') {
                    $bkg = '#C3AD93';
                } elseif($background_color == 'dark-blue') {
                    $bkg = '#2D4B62';
                } elseif($background_color == 'seafoam-green') {
                    $bkg = '#91ACA9';
                }
            $date_string = get_field('date', $p->ID); 
            $date = DateTime::createFromFormat('Ymd', $date_string);
            $name = get_the_title($p->ID);
            $start_time = get_field('start_time', $p->ID);
            $description = get_field('description', $p->ID);
            $event_page_link = get_field('event_page_link', $p->ID);
            $register_link = get_field('register_link', $p->ID);
            ?>
            <div class="large-3 small-12 columns">
                <div class="event section--one__content__event" <?php echo 'style="background-color:'.$bkg.';"'; ?>>
                    <?php
                    if($date) echo '<div class="name section--one__content__event__day">'.$date->format('j').'</div>';
                    if($name) echo '<div class="name section--one__content__event__name">'.$name.'</div>';
                    if($start_time) echo '<div class="name section--one__content__event__time">'.$start_time.'</div>';
                    if($description) echo '<div class="position section--one__content__event__description">'.$description.'</div>';
                    if($event_page_link) echo '<a href="'.$event_page_link.'" target="_blank" class="link section--one__content__event__link">EVENT PAGE</a>'; 
                    if($event_page_link && $register_link) echo ' <span class="link section--one__content__event__link">|</span> ';
                    if($register_link) echo '<a href="'.$register_link.'" target="_blank" class="link section--one__content__event__link">REGISTER</a>';
                    ?>
                </div>
            </div>
            <?php 
            endforeach; 
            
            endif; 
            ?>