M
M
Muvka2018-05-05 14:15:29
WordPress
Muvka, 2018-05-05 14:15:29

Why is WordPress pagination not showing up?

There is a page template. It displays posts from a custom post type. I'm trying to display pagination

<?php the_posts_pagination( array(
  'show_all'     => true,
  'prev_next'    => false,
  'add_args'     => false,
  'add_fragment' => '',
) ); ?>

Outputs nothing. Apparently due to the fact that $GLOBALS['wp_query']->max_num_pages returns 0. Posts 4 at
'posts_per_page' => '2',
'posts_per_archive_page' => '2',

I found a pattern - If the code is inserted directly into the page, then pagination starts working. If through add and do_action - that does not work. Need some kind of global variable? Tell me which one.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Max Medar, 2018-05-05
@MedVedar

Hey!
I will give a working piece of code where I solved this problem for myself.

$term      = get_queried_object();
          $term_slug = $term->slug;
          $paged     = ( get_query_var( 'page' ) ) ? get_query_var( 'page' ) : 1;
          $_posts    = new WP_Query( array(
            'post_type'      => 'product',
            'posts_per_page' => 10,
            'paged'          => $paged,
            'tax_query'      => array(
              array(
                'taxonomy' => 'product-category',
                'field'    => 'slug',
                'terms'    => $term_slug,
              ),
            ),
          ) );
          global $wp_query;
          $tmp_query = $wp_query;
          $wp_query  = null;
          $wp_query  = $_posts;
          if ( $_posts->have_posts() ) :
            while ( $_posts->have_posts() ) :
              $_posts->the_post();
              get_template_part( 'template-parts/content-product', 'preview' );
            endwhile;
            the_posts_pagination( array(
              'type' => 'list',
              'prev_text'    => '<i class="fas fa-angle-double-left"></i>',
              'next_text'    => '<i class="fas fa-angle-double-right"></i>',
            ) );
          else :
            get_template_part( 'template-parts/content', 'none' );
          endif;
          $wp_query = null;
          $wp_query = $tmp_query;
          wp_reset_postdata();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question