Pagination not working on my archive page for a custom post type

Question

First off I have a custom post type called events, and I wrote a custom query in in my archive-events.php file. Everything is working great except for the pagination.

To trouble shoot I stripped everything out except for the essentials and pagination was working Perfectly. After a lot of googling I think my issue might be that I need to write my query as a function in the functions.php file. Something like this:

function set_posts_per_page_for_events_cpt( $query ) {
  if ( !is_admin() && $query->is_main_query() && is_post_type_archive( 'events' ) ) {
    $query->set( 'posts_per_page', '5' );
  }
}
add_action( 'pre_get_posts', 'set_posts_per_page_for_events_cpt' );

I’m totally fine with this, but I’m not sure how to rewrite my query so that it works. This is my query:

<?php
$currentdate = date("Y-m-d",mktime(0,0,0,date("m"),date("d"),date("Y")));

$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;

$wp_query = new WP_Query(  array (
'post_type' => 'events',
'meta_query'=> array(
    array(
       'key' => 'event_date',
       'compare' => '>',
       'value' => $currentdate,
       'type' => 'DATE',
    )),
    'meta_key' => 'event_date',
    'orderby' => 'meta_value',
    'order' => 'ASC',
    'posts_per_page' => 12,
    'paged' => $paged,
    )
); ?>

<?php if( $wp_query->have_posts() ): ?>
<?php while( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
<!-- All My Events -->
<?php endwhile;?>
<?php echo paginate_links(); ?>
<?php endif; ?>

If I’m totally wrong on this please let me know. Any help or a point into the right direction is greatly appreciated.

0
, , , , Mike 5 years 2017-01-31T16:03:26-05:00 0 Answers 60 views 0

Leave an answer

Browse
Browse