WordPress custom post type pagination 404 error

I am trying to get pagination working on a custom theme.

I have a post type with custom taxonomies and I have pagination for those custom taxonomies and that works fine.

Read More

However on another part of the site I have another custom post type; I am trying to paginate through the posts in this post type. Currently the posts are in a custom taxonomy also in this post type, the custom taxonomy behaves like a category.

The URL structure is http://www.domain.com/latest-news/, however when I go to http://www.domain.com/latest-news/2/ I get a 404 error, this is the problem.

Here is my code (sorry it’s so long):

<?php
                $numCats = 0;
                $paged = (get_query_var('page')) ? get_query_var('page') : 1;
                $posts_per_page = 5;
                $offset = ($posts_per_page * $paged - $posts_per_page);
                $taxonomies = array(
                    'latest-cat'
                    );
                $args = array(
                  'hide_empty' => false,
                  'parent' => 0,
                  'orderby' => 'id',
                  'order' => 'DESC',
                  'paged' => $paged
                  );
                $terms = get_terms($taxonomies, $args);
                foreach ($terms as $term) {
                    $args = array(
                    'post_type' => 'latest_news_item',
                    'oserby' => 'id',
                    'order' => 'ASC',
                    'posts_per_page' => -1,
                    'tax_query' => array(
                      array(
                        'taxonomy' => 'latest-cat',
                        'terms' => $term->term_id
                        )
                      )
                    );
                    $wp_query = new WP_Query($args);
                    while ($wp_query->have_posts() ) : $wp_query->the_post();
                    $numCats++;
                    endwhile;
                    $args = array(
                    'post_type' => 'latest_news_item',
                    'parent' => 115,
                    'orderby' => 'id',
                    'order' => 'ASC',
                    'posts_per_page' => $posts_per_page,
                    'offset' => 0,
                    'paged' => $paged
                    );
                    $wp_query = new WP_Query($args);
                while ($wp_query->have_posts() ) : $wp_query->the_post();
                ?>
                <div class="news-events-item">
                    <h3><?php echo get_the_title(); ?></h3>
                    <?php $content = get_the_content(); ?>
                    <p class="summary"><?php echo wp_trim_words($content, '30', '...'); ?><a href="<?php echo get_the_permalink(); ?>" class="read-more">More</a></p>
                </div>
                <?php
                endwhile;
                } ?>

The $numCats variable is just to count the number of posts so that I can set up the pagination links, and that part works.

Thank you

Related posts

Leave a Reply

1 comment

  1. I have fixed this for myself in this way:

    On an archive page of the custom post type “blog”.

                    $paged = 1;
                    if ( get_query_var('paged') ) $paged = get_query_var('paged');
                    if ( get_query_var('page') ) $paged = get_query_var('page');
    
                    query_posts( '&post_type=blog&paged=' . $paged );
    
                    $args = array(
                        'post_type' => 'blog',
                        'paged' => $paged,
                        'orderby'           => 'date',
                        'order'             => 'DEST',
                        'posts_per_page'                => '1',
    
                    );
                    $loop = new WP_Query( $args );
                    while ( $loop->have_posts() ) : $loop->the_post();
    
    • this is the classic loop with added arguments for post type “blog” and added “Paged” attribute.
      And obviously these points:

      $paged = 1;
                    if ( get_query_var('paged') ) $paged = get_query_var('paged');
                    if ( get_query_var('page') ) $paged = get_query_var('page');
      
                    query_posts( '&post_type=blog&paged=' . $paged );
      

    At the end, outside of the loop is the pagination output from functions.php

                <?php echo pagination($loop->max_num_pages); ?>
    

    In functions.php is the permalink modification and also modification of the “posts_per_page” set to ONE post, so you can test the pagination.

    function paginate() {
        global $wp_query, $wp_rewrite;
        $wp_query->query_vars['paged'] > 1 ? $current = $wp_query->query_vars['paged'] : $current = 1;
        $pagination = array(
            'base' => @add_query_arg('page','%#%'),
            'format' => '',
            'total' => $wp_query->max_num_pages,
            'current' => $current,
            'show_all' => true,
            'type' => 'plain'
        );
        if ( $wp_rewrite->using_permalinks() ) $pagination['base'] = user_trailingslashit( trailingslashit( remove_query_arg( 's', get_pagenum_link( 1 ) ) ) . 'page/%#%/', 'paged' );
        if ( !empty($wp_query->query_vars['s']) ) $pagination['add_args'] = array( 's' => get_query_var( 's' ) );
        echo paginate_links( $pagination );
    }
    function portfolio_posts_per_page( $query ) {
        if ( $query->query_vars['post_type'] == 'blog' ) $query->query_vars['posts_per_page'] = 1;
        return $query;
    }
    if ( !is_admin() ) add_filter( 'pre_get_posts', 'portfolio_posts_per_page' );
    And as last here is the pagination code which is used on the archive page outside of the loop.
    
    **used with bootstrap 5.0**
    
    
        
    
    function pagination($pages = '', $range = 1)
    {
        $showitems = ($range * 2) + 1;
    
        global $paged;
        if (empty($paged)) $paged = 1;
    
        if ($pages == '') {
            global $the_query;
        $pages = $the_query->max_num_pages;
        if (!$pages) {
            $pages = 1;
        }
    }
    
    if (1 != $pages) {
        echo "<nav aria-label="...">
            <ul class="pagination">";
        if ($paged > 2 && $paged > $range + 1 && $showitems < $pages)
            echo "
                     <li class="page-item ">
                  <a class="page-link" href="" . get_pagenum_link(1) . "">První</a>
                </li>";
        if ($paged > 1 && $showitems < $pages)
    
            echo "
                  <li class="page-item ">
                  <a class="page-link" href="" . get_pagenum_link($paged - 1) . "">Předchozí</a>
                </li>";
    
        for ($i = 1; $i <= $pages; $i++) {
            if (1 != $pages && (!($i >= $paged + $range + 1 || $i <= $paged - $range - 1) || $pages <= $showitems)) {
                 echo ($paged == $i) ? "<li class="page-item active"><a class="page-link" href="" . get_pagenum_link($i) . "">" . $i . "</a></li>" : "<li class="page-item"><a class="page-link" href="" . get_pagenum_link($i) . "">" . $i . "</a></li>";
    
            }
        }
    
        if ($paged < $pages && $showitems < $pages)
           echo "
                  <li class="page-item ">
                  <a class="page-link" href="" . get_pagenum_link($paged + 1) . "">Další</a>
                </li>";
        if ($paged < $pages - 1 && $paged + $range - 1 < $pages && $showitems < $pages)
            echo "
                     <li class="page-item ">
                  <a class="page-link" href="" . get_pagenum_link($pages) . "">Poslední</a>
                </li>";
        echo "</ul>";
        echo "</div>";
    
        //echo "<div class='all_pages text-muted'><small>Stránek " . $pages . "</small></div>";
    }
    

    }

    I hope it helps, it works for me.

    (sorry for formating, stack overflow editor is totall bull…)