How to include pagination in a Wordpress Custom Post Type Query

70,231

Solution 1

Try the code below:

    $the_query = new WP_Query( array('posts_per_page'=>30,
                                 'post_type'=>'phcl',
                                 'paged' => get_query_var('paged') ? get_query_var('paged') : 1) 
                            ); 
                            ?>
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
<div class="col-xs-12 file">
<a href="<?php the_permalink(); ?>" class="file-title" target="_blank">
<i class="fa fa-angle-right" aria-hidden="true"></i> <?php echo get_the_title(); ?>
</a>
<div class="file-description"><?php the_content(); ?></div>
</div>
<?php
endwhile;

$big = 999999999; // need an unlikely integer
 echo paginate_links( array(
    'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
    'format' => '?paged=%#%',
    'current' => max( 1, get_query_var('paged') ),
    'total' => $the_query->max_num_pages
) );

wp_reset_postdata();

Solution 2

When querying a loop with new WP_Query set the 'total' parameter to the max_num_pages property of the WP_Query object.

Example of a custom query:

<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

$args = array(
    'post_type'=>'post', // Your post type name
    'posts_per_page' => 6,
    'paged' => $paged,
);

$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
    while ( $loop->have_posts() ) : $loop->the_post();

             // YOUR CODE

    endwhile;

    $total_pages = $loop->max_num_pages;

    if ($total_pages > 1){

        $current_page = max(1, get_query_var('paged'));

        echo paginate_links(array(
            'base' => get_pagenum_link(1) . '%_%',
            'format' => '/page/%#%',
            'current' => $current_page,
            'total' => $total_pages,
            'prev_text'    => __('« prev'),
            'next_text'    => __('next »'),
        ));
    }    
}
wp_reset_postdata();
?>

Example of paginate_links parameters adapted to the custom query above:

For more reference please visit this link

Share:
70,231
Ganikkost
Author by

Ganikkost

Updated on May 05, 2021

Comments

  • Ganikkost
    Ganikkost about 3 years

    I have the code below:

    <?php $the_query = new WP_Query( 'posts_per_page=30&post_type=phcl' ); ?>
    
    <?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
    
    <div class="col-xs-12 file">
        <a href="<?php echo $file; ?>" class="file-title" target="_blank">
            <i class="fa fa-angle-right" aria-hidden="true"></i> 
            <?php echo get_the_title(); ?>
        </a>
        <div class="file-description">
            <?php the_content(); ?>
        </div>
    </div>
    <?php endwhile; wp_reset_postdata(); ?>
    

    I am trying to use paginate_links Wordpress function but no matter where I put it, I can't make it work. Can someone help me with this?

  • Shiv Singh
    Shiv Singh over 6 years
    Use <?php the_permalink(); ?> in place of <?php echo $file; ?>
  • Pixelsmith
    Pixelsmith about 5 years
    There's a small error in this solution. The code 'paged' => get_query_var('paged') ? get_query_var('paged') : 1) should not have a final parenthesis. In order for the pagination to run correctly remove the closing parenthesis from after the number 1.
  • Khom Nazid
    Khom Nazid almost 5 years
    Thank you for sharing this. It does show me the pagination. But when I click on the URL for Page 2, it still shows me the same content as Page 1. Any ideas?
  • Purvik Dhorajiya
    Purvik Dhorajiya almost 5 years
    @KhomNazid You are following this permalink structure screencast.com/t/IeYH7bxY3 Right?
  • Khom Nazid
    Khom Nazid almost 5 years
    @PurvikDhorajiya, no. I'm following %category%/%postname%, which we do need for our blog section outside the custom posts. For custom posts, we rely on the "Permalink Manager Lite" plugin. Our specific case is detailed here (and now our page 2 gives a 404, actually): stackoverflow.com/questions/57301662/…
  • Nadia
    Nadia over 3 years
    query for pagination doesn't work, when i click the second page t returns the first page posts
  • Nadia
    Nadia over 3 years
    @Jay no it didnt
  • mjcoder
    mjcoder about 3 years
    This is perfect!! @PurvikDhorajiya
  • Mr Toad
    Mr Toad over 2 years
    This works well for me thank you, however, it always takes you to the top of the page like you are navigating between different pages, where as I would like it to stay in the same position and just appear to paginate through the post section. Any ideas? Thanks