22

I came across the /page/2 404 pagination problem and tried to find a solution. I've read some suggestions, but it didn't help yet.

Working: http://pandasnacozinha.com.br/bolos-doces-e-sobremesas/

Not working: http://pandasnacozinha.com.br/bolos-doces-e-sobremesas/page/2/

I've been trying to figure this out for hours. I understand WordPress assumes "page" is a post from the category "bolos, doces e sobremesas" since my permalink structure is /%category%/%postname%/ but I don't know how to fix it.

5
  • Are you running your own query in the template? This typically happens when you incorrectly overwrite the query in the template to change posts_per_page or some other query parameter.
    – Milo
    Nov 24, 2015 at 17:20
  • I don't remember touching a single line of code. How could I check this? Nov 24, 2015 at 17:22
  • If you haven't modified the theme and the problem persists with all plugins disabled, then you should ask the person who created the theme.
    – Milo
    Nov 24, 2015 at 17:26
  • Thanks for your time, Milo. I simply installed a plugin that solved the problem. I will answer this question and provide the link. Nov 27, 2015 at 19:48
  • Simply changing the pagination parameter from "paged" to "page" fixed the issue in my case.
    – user54002
    Mar 17, 2020 at 13:27

13 Answers 13

26

I had the same problem and I noticed that in the 'posts_per_page = 6' and 'Settings/Reading on 'options-reading' WordPress argument, I was set to 10. When I put everything to the same value (6 in my case) everything started working again.

7
  • Life saver @jeferson, thank you
    – Ana DEV
    Apr 28, 2020 at 6:06
  • @Jeferson Padilha this worked beautifully!!!
    – Alejo_Blue
    Jan 19, 2021 at 19:57
  • 2
    Where is posts_per_page set?
    – Keyslinger
    Mar 3, 2021 at 5:52
  • This post inspired me, and found the issue why it's happening. You saved my day!
    – Felix Htoo
    Jun 9, 2022 at 10:27
  • This totally solved it for me. I still don't understand the problem. My custom 'posts_per_page' is set to 9.
    – Kalnode
    Oct 31, 2022 at 12:43
25

In my case with custom links: /%category%/%postname%/ I had a problem with: /news/page/2/

And finally this works for me (add to functions.php):

function remove_page_from_query_string($query_string)
{ 
    if ($query_string['name'] == 'page' && isset($query_string['page'])) {
        unset($query_string['name']);
        $query_string['paged'] = $query_string['page'];
    }      
    return $query_string;
}
add_filter('request', 'remove_page_from_query_string');
3
  • Thanks, man. It was really helpful. Mar 20, 2021 at 19:14
  • Brilliant. worked like a charm. thanks!
    – lStoilov
    Sep 2, 2021 at 7:50
  • Just had the exact same case - seems like a big oversight to allow customizing the permalink structure and not giving any hint to the users that they might have to overwrite rewrites manually... Or maybe it's rather a bug and shouldn't be necessary? Nov 22, 2021 at 13:43
16

Tried several hours, until I found a working solution in this article.

In your functions.php file, add

/**
 * Fix pagination on archive pages
 * After adding a rewrite rule, go to Settings > Permalinks and click Save to flush the rules cache
 */
function my_pagination_rewrite() {
    add_rewrite_rule('blog/page/?([0-9]{1,})/?$', 'index.php?category_name=blog&paged=$matches[1]', 'top');
}
add_action('init', 'my_pagination_rewrite');

Replace blog with your category name in the code above.

After adding this code, make sure you go to Settings > Permalinks and click Save to flush the rules cache, or else the rule will not be applied.

Hope this helps!

2
  • 1
    This did it for me. Would still love to know the 'why' of this behavior popping up out of nowhere. But thx for the fix.
    – dashard
    Apr 26, 2019 at 19:49
  • Thank you! Why this bug still exist in 2020?
    – skywind
    Dec 17, 2020 at 22:40
2

I found changing the permalink structure work for me, look:

The permalink was like this in custom structure: /index.php/%year%/%monthnum%/%day%/%postname%/

Then I changed it to: Day and name (just select the radio button) and it will look like this: /%year%/%monthnum%/%day%/%postname%/

I tried this and it works!

1
  • I found that simply loading the permalink page cleared the issue May 10, 2019 at 21:09
0

my solution is in 3 step:

1- the first one : Installing this plugin : https://wordpress.org/plugins/category-pagination-fix/

2-then : mach your cod with this structure

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

        $q=new wp_Query(
            array(
                 "posts_per_page"=>10,
                 "post_type"=>"",
                 "meta_key"=>"",
                 "orderby"=>"meta_value_num",
                 "order"=>"asc",
                 "paged" => $paged,

            )
        );
 while($q->have_posts())

        {
            $q->the_post();    
            ?>
<li></li>
 <?php
        }
        wp_reset_postdata();

        ?>





<div class="pagination">
<?php
global $q;
$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' => $q->max_num_pages
) );
?>
</div>

3-go in wordpress setting > reading > most number of posts per page of blog then input number 1

0

As others had mentioned. Make sure your 'posts_per_page = 6' is equal or less than the Wordpress Settings > Reading > Blog pages show at most setting.

I ran into this issue recently and the issue was that I control the posts per page completely from the template file. If There are not enough posts for the final page it will 404.

for example. I have my posts per page set to 9, but I had 25 posts. page one and 2 would work but page 3 would 404. I set the WordPress setting to 1 and left my template file at 9 and it is now functioning as expected.

0

In my case, using Divi and the PageNavi plugin, the reason I was getting the 404 was that the page template (archive in my case) was getting the paged query parameter using get_query_var( 'paged' ), instead all I had to do was use the global variable like below:

<?php
/*
Template Name: Archives
*/
get_header(); ?>

<?php

    // $paged is a global variable provider by the theme?
    global $paged;

    $args = array(
        'posts_per_page' => 4, 
        'post_type' => 'axis',
        'paged' => $paged,
    );

    $myposts = new WP_Query($args);
?>

<div div style="width: 50%; margin: 0 auto;">

    <?php while ( $myposts->have_posts() ) : $myposts->the_post(); ?>

            <div class="media-body">
                <?php the_content(); ?>
            </div>


    <?php endwhile; wp_reset_postdata(); ?>

    <?php wp_pagenavi( array( 'query' => $myposts ) ); ?>

</div>

<?php get_sidebar(); ?>
<?php get_footer(); ?>

This code corrected linked and resolved post-name/page/N formatter posts. The permalink setting is set to "Post Name" i.e. http://localhost/sample-post/

0

In my case I had this in a loop:

if (is_category()) $args['posts_per_page'] = 8;

And in Settings->Reading I had 10 posts per page for blog and syndication

I changed to 8 posts per page in Settings->Reading and now the 404 disappeared and everything seems to work.

I have no idea why but probably this could help someone in the future

0

Here's a generalization of kregus's answer that fixes all categories at once:

/**
 * Fix pagination on archive pages
 */
function my_pagination_rewrite() {
    $categories = '(' . implode('|', array_map(function($category){return $category->slug;}, get_categories())) . ')';
    add_rewrite_rule($categories . '/page/?([0-9]{1,})/?$', 'index.php?category_name=$matches[1]&paged=$matches[2]', 'top');
}
add_action('init', 'my_pagination_rewrite');
0

For those who are using WooCommerce; I was looking for the same thing, and found out WooCommerce changes the posts_per_page for product categories. See: https://woocommerce.com/document/change-number-of-products-displayed-per-page/

By using the WooCommerce filter:

/**
 * Change number of products that are displayed per page (shop page)
 */
add_filter( 'loop_shop_per_page', 'wc_update_loop_shop_per_page', 20 );

function wc_update_loop_shop_per_page( $cols ) {
  // $cols contains the current number of products per page based on the value stored on Options –> Reading
  // Return the number of products you wanna show per page.
  $cols = 9;
  return $cols;
}

In my case I changed the $cols to 12 and then the pagination was working fine again.

0

I had this issue too, tried all of the other solutions on this page, nothing helped. In my case, this was caused by having the 'Category base' the same as my blog page permalink.

E.g. my blog page permalink was 'articles' and I had set the 'Category base' (in Settings > Permalinks) set to 'articles' too. Changing the 'Category base' to something else, fixed the 404s.

-1

I'm getting the error 404 when I head back to the url with /page/2/.... So I go to my WordPress 404 page and add this javascript on the top of the error page:

<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<script>
   var getreurl = window.location.href;
   if(getreurl != ""){
     var res = getreurl.split("/?");
       if(res[1] != ""){
         var resd = "http://www.yourwebsite.com/list/?"+res[1];
         window.location = resd;
     }
 }
 </script>

When I head back to the 404 page it carries the URL, I parse the URL and get URL string that I need and rebuild a new link then redirect on the new rebuilt link.

1
  • 1
    JS based solution will still give 404 to google/facebook etc. There is no reason not to run this kind of code on the php side Jan 19, 2017 at 7:43
-3

Found the solution!

Installing this plugin solved the problem: https://wordpress.org/plugins/category-pagination-fix/

I also had to make changes in Settings > Reading to show less than 10 posts per page (I'm showing 6 posts per page now, but anything below 10 should work).

Hope it works for you all. :)

2
  • 1
    an outdated plugin fixing a specific problem with wordpress which was fixed in core 6 years ago? unlikely this is a good answer Aug 20, 2017 at 5:03
  • 2
    The plugin was by default a no-go for me, but you pointed me to the right direction with the Settings change! Thanks!
    – daneczech
    Mar 21, 2018 at 16:21

Not the answer you're looking for? Browse other questions tagged or ask your own question.