Why is my pagination for custom post type not working?

Here is the code I’m using to display the pagination…

<?php

global $wp_query;



$big = 999999999; // need an unlikely integer



echo paginate_links( array(

'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),

'format' => '?paged=%#%',

'current' => max( 1, get_query_var('paged') ),

'total' => $wp_query->max_num_pages

) );

?>

But when you click next and got to /page/2/ it says “Page Not Found”

Read More

what am I doing wrong???

Related posts

Leave a Reply

1 comment

  1. The code above is not working because it is not set for a custom post type.
    If you look in the wordpress codex for paginate_links you will find your code under the

    Basic Example
    To add pagination to your search results and archives, you can use the following example

    and is not working for your query because you have different query_vars, the code you should work with should be from the same codex page:

    Example With a Custom Query
    When querying a loop with new WP_Query set the ‘total’ parameter to the max_num_pages property of the WP_Query object.
    with the query beeing:

    <?php
    $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
    $args = array(
    'posts_per_page' => 5,
    'category_name' => 'gallery',
    'paged' => $paged,
    );
    $the_query = new WP_Query( $args );
    ?>
    <!-- the loop etc.. -->
    

    and the pagination code:

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

    When working with pagination “Page not found” errors usually are caused by missuning the query vars.