How to create past event page with custom query and pagination

 In this post we will create past event page and use custom query and pagination.

First go to wordpress dashboard and create new page Past Events and publish without content.

Now go to theme folder and create new page name page-past-events.php 

Now copy archive-event.php code and paste into page-past-events.php and change title Past Events

Now copy custom query from front-page.php and paste in this page-past-events.php.

like below ->

 $today = date('Ymd');
        $pastEvents = new WP_Query(array(
            'post_type' => 'event',
            'meta_key' => 'event_date',
            'orderby' => 'meta_value_num',
            'order' => 'ASC',
            'meta_query' => array(
                array(
                  'key' => 'event_date',
                  'compare' => '<',
                  'value' => $today,
                  'type' => 'numeric'
                )
                ),

            ));

and save.

Now add pagination in this.

and for pagination add below code in bottom for pagination

 echo paginate_links(array(
     'total' => $pastEvents->max_num_pages,
  ));

and in Custom query array add below line -> 

 'paged' => get_query_var('paged', 1),

and save.

Add link for past event in archive-event.php

go to bottom of page before last div add below code 

<hr class="section-break">
  <p>Looking for recap of past events? <a href="<?php echo site_url('/past-events')  ?>">Check out our past event
      archive</a></p>

and save.

after also update events link in header.php like below code 

 <li <?php if(get_post_type() == 'event' || is_page('past-events')) echo 'class="current-menu-item"'; ?>><a
                href="<?php echo get_post_type_archive_link('event');  ?>">Events</a></li>


Comments

Popular posts from this blog

Basics Of PHP

Creating App.js for Node Application

WordPress Full Guide For Developer