10

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.

  • 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 '15 at 17:20
  • I don't remember touching a single line of code. How could I check this? – Giovanna Cóppola Nov 24 '15 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 '15 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. – Giovanna Cóppola Nov 27 '15 at 19:48
  • Simply changing the pagination parameter from "paged" to "page" fixed the issue in my case. – user54002 Mar 17 at 13:27

10 Answers 10

10

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.

| improve this answer | |
  • Life saver @jeferson, thank you – Anahit DEV Apr 28 at 6:06
7

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!

| improve this answer | |
  • 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 '19 at 19:49
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!

| improve this answer | |
  • I found that simply loading the permalink page cleared the issue – jerrygarciuh May 10 '19 at 21:09
2

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');
| improve this answer | |
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

| improve this answer | |
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.

| improve this answer | |
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/

| improve this answer | |
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

| improve this answer | |
-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.

| improve this answer | |
  • 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 – Mark Kaplun Jan 19 '17 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. :)

| improve this answer | |
  • 1
    an outdated plugin fixing a specific problem with wordpress which was fixed in core 6 years ago? unlikely this is a good answer – Mark Kaplun Aug 20 '17 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 '18 at 16:21

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