0

I'm using a page as archive for a custom post type called event.

register_post_type('event', array(
    'labels' => array(
        'name' => 'Événements',
        'singular_name' => 'Événement',
    ),
    'public' => true,
    'has_archive' => false,
    'hierarchical' => false,
    'menu_icon' => 'dashicons-calendar-alt',
    'rewrite' => array(
        'with_front' => false,
        'slug' => 'evenements'
    )
));

Then, I defined this page template to template-event.php, where I use the following code to loop through post types.

$args = array(
    'post_type'      => 'event',
    'posts_per_page' => 6,
    'paged'          => get_query_var('paged')
);

$the_query = new WP_Query( $args );

if( $the_query->have_posts() ) :

    while ( $the_query->have_posts() ) : $the_query->the_post();

        the_title();

    endwhile;

    wp_pagenavi( array( 'query' => $the_query ) );
    wp_reset_postdata();

endif;
?>

This code works great for my home.php page, but not for this one.
Any idea why?

5
  • Is there a reason you are creating a new query in the template instead of using the default main query? This is the source of your problem- you can’t paginate a custom query beyond the number of pages in the main query in an archive.
    – Milo
    Commented Jan 10, 2019 at 23:21
  • @Milo I'm using a new query because I need to order posts and use meta_query for showing only those which have a ACF date bigger than the current date.
    – Quentin
    Commented Jan 11, 2019 at 13:06
  • Use pre_get_posts to alter main query arguments.
    – Milo
    Commented Jan 11, 2019 at 13:23
  • @milo The default main query doesn't work since I'm using a page as archive.
    – Quentin
    Commented Jan 11, 2019 at 22:06
  • Pagination pattern for singular views is slug/n/, not slug/page/n/, and query var is page what nstead of paged
    – Milo
    Commented Jan 12, 2019 at 0:20

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.