adding pagination to custom wp_query

you can add this to your custom wp_query making sure your query also has the paging passed through to it

PHP

<!-- Main Query and Loop -->
<?php $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;?>

<?php $wpb_all_query = new WP_Query(array('post_type'=>'post', 'post_status'=>'publish', 'posts_per_page'=>8, 'paged' => $paged)); ?>

<?php if ( $wpb_all_query->have_posts() ) : ?>
<!-- the loop -->
<?php while ( $wpb_all_query->have_posts() ) : $wpb_all_query->the_post(); ?>

<div class='post-title'><?php the_title(); ?></div>
<a href="<?php the_permalink(); ?>"></a>
<hr />

<?php endwhile; ?>
<!-- end of the loop -->

<div class="pagination">
  <?php
              echo paginate_links( array(
                  'base'         => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
                  'total'        => $wpb_all_query->max_num_pages,
                  'current'      => max( 1, get_query_var( 'paged' ) ),
                  'format'       => '?paged=%#%',
                  'show_all'     => false,
                  'type'         => 'plain',
                  'end_size'     => 2,
                  'mid_size'     => 1,
                  'prev_next'    => true,
                  'prev_text'    => sprintf( '<i></i> %1$s', __( 'Newer Posts', 'text-domain' ) ),
                  'next_text'    => sprintf( '%1$s <i></i>', __( 'Older Posts', 'text-domain' ) ),
                  'add_args'     => false,
                  'add_fragment' => '',
              ) );
          ?>
</div>

<?php wp_reset_postdata(); ?>

<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>

External Link for adding pagination to custom wp_query

Page Views

This page has been viewed 86 times.

Search Code
Search Code by entering your search text above.
Welcome

This is my test area for webdev. I keep a collection of code snippits here, mostly for my reference. Also if i find a good site, i usually add it here.

âĪðŸ‘Đ‍ðŸ’ŧðŸ•đ

Random Quote
You drown not by falling in the river, but by staying submerged.
Unknown