I am trying to make a pagination, so far it is showing page links properly like 1 2 3 4 > but the problem is when i navigate to page 2 or any other page it take me to 404 page. Here is my pagination function

if ( ! function_exists( 'sp_pagination' ) ) { 

    function sp_pagination( $query = '', $echo = true ) {


        $prev_arrow = is_rtl() ? 'fa fa-angle-right' : 'fa fa-angle-left';
        $next_arrow = is_rtl() ? 'fa fa-angle-left' : 'fa fa-angle-right';

        // Get global $query
        if ( ! $query ) {
            global $wp_query;
            $query = $wp_query;
        }

        // Set vars
        $total  = $query->max_num_pages;
        $big    = 999999999;

        // Display pagination
        if ( $total > 1 ) {

            // Get current page
            if ( $current_page = get_query_var( 'paged' ) ) {
                $current_page = $current_page;
            } elseif ( $current_page = get_query_var( 'page' ) ) {
                $current_page = $current_page;
            } else {
                $current_page = 1;
            }

            // Get permalink structure
            if ( get_option( 'permalink_structure' ) ) {
                if ( is_page() ) {
                    $format = 'page/%#%/';
                } else {
                    $format = '/%#%/';
                }
            } else {
                $format = '&paged=%#%';
            }

            $args = apply_filters( 'sp_pagination_args', array(
                'base'      => str_replace( $big, '%#%', html_entity_decode( get_pagenum_link( $big ) ) ),
                'format'    => $format,
                'current'   => max( 1, $current_page ),
                'total'     => $total,
                'mid_size'  => 3,
                'type'      => 'list',
                'prev_text' => '<span class="' . $prev_arrow . '"></span>',
                'next_text' => '<span class="' . $next_arrow . '"></span>',
            ) );

            // Output pagination
            if ( $echo ) {
                echo '<div class="sp-pagination">' . paginate_links( $args ) . '</div>';
            } else {
                return '<div class="sp-pagination">' . paginate_links( $args ) . '</div>';
            }

        }

    }

}

And here is my query

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

        $portfolio_grid_query = new WP_Query( 
            array( 
                'post_type'             => 'portfolio',
                'post_status'           => 'publish',
                'name'                  => $portfolio_grid_title_slug,
                'posts_per_page'        => $portfolio_grid_item_limit,
                'category_name'         => $portfolio_grid_cats,
                'order'                 => $portfolio_order,
                'orderby'               => $portfolio_orderby,
                'paged'                 => $paged
            ) 
        );

here all the while loop and post structure goes and after while loop ends i put the pagination function like so:

sp_pagination($portfolio_grid_query, true);

It is showing all the navigation structure properly but the issue is when i navigate it takes me to 404 page instead of page 2 to show other remaining posts. Keep in mind i have total of 9 posts and i set posts_per_page to 6, which is showing me 6 posts on current page and if i click on page 2 link it should take me to other page to show remaining 3 posts.

Read more here: Pagination not working for custom post type


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: