• I have a category that consists of 14 pages. But there is a problem. Once you reach /page/14/, you can open blank pages 15, 16, 17, 20, 100, 200 and so on. I would like after page 14, visitors to be able to only see “404 not found”. How can I do that?

    <?php
        $wp_query = new WP_Query(array(
                'category_name' => 'blog',
                'post_status' => 'publish',
                'orderby' => 'date',
                'order' => 'DESC',
                'posts_per_page' => 20,
                'paged' => get_query_var('paged')
        ));
        if ( get_previous_posts_link() ) { ?>
    <link rel="prev" href="<?php echo get_pagenum_link( $paged - 1 ); ?>" /><?php
        }
        if ( get_next_posts_link() ) { ?>
    
    <link rel="next" href="<?php echo get_pagenum_link( $paged +1 ); ?>" /><?php
        }
    ?>
Viewing 7 replies - 1 through 7 (of 7 total)
  • Moderator bcworkz

    (@bcworkz)

    Pagination functgions should be aware of how many pages there are for a particular query and not provide further links. While serving a 404 response is better than blank pages, it’s still not good for UX or SEO. The pagination links you are using are actually trying to paginate the original query, which apparently has more pages than your new WP_Query.

    Instead of making a new query, if you are not using the original query, modify the original through the “pre_get_posts” action to get what you want. Don’t make a new query. Then the pagination links will work correctly.

    If you must have a new query for some reason that you want to paginate, you have to use paginate_links(). Other pagination functions will not work correctly.

    Thread Starter tw8sw8dw8

    (@tw8sw8dw8)

    I tried to use with pre_get_posts, but obviously doesn’t work because I use category page template. Could you please make an example?

    Moderator bcworkz

    (@bcworkz)

    The template used wouldn’t affect the efficacy of “pre_get_posts”. You can stick with a new query to avoid having to learn and debug something new, but you then need to use paginate_links(). There are examples of proper usage near the bottom in the user notes:
    https://developer.wordpress.org/reference/functions/paginate_links/

    Thread Starter tw8sw8dw8

    (@tw8sw8dw8)

    If I remove the “$wp_query = new WP_Query(array…” code from the page template, there is no posts at all. The pre_get_posts function can only manipulate the posts per page. The empty (blank) /page/1000000/ still exist.
    Do I miss something?

    Moderator bcworkz

    (@bcworkz)

    “pre_get_posts” should be able to alter the query in any way that you could by supplying args to a new WP_Query. It’s just setting query vars through a different mechanism. They’re the same query vars either way. There is a bit of a learning curve, so having difficulty with it isn’t unusual. Your only choices are to use “pre_get_posts” or stick with a new WP_Query and use paginate_links() for the pagination links.

    “page/1000000/”?? Wait a minute, where are these bogus links coming from? Are you manually typing them or are there actually links like that somewhere on you site? Either way, you should get a nothing found page, but it could impact where we focus our attention.

    Thread Starter tw8sw8dw8

    (@tw8sw8dw8)

    Here is exactly what I do:

    function show_category_posts( $query ) {
        if ( $query->is_main_query() ) {
            $query->set( 'category_name', 'blog' );
            $query->set( 'posts_per_page', 20 );
        }
    }
    add_action( 'pre_get_posts', 'show_category_posts' );

    Then in the page template:

    <?php if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); ?>
    <h2><?php the_title(); ?></h2>
    <?php endwhile; ?>
    <?php wp_reset_postdata(); ?>
    <?php endif; ?>

    And the result is nothing.

    Moderator bcworkz

    (@bcworkz)

    Ah, OK. You should have an else: condition forif (have_posts()) : that outputs some sort of nothing found message. When requesting a page in excess of those available, that message is shown and the request’s reply status should be 404. You don’t get the standard 404 template page, but the server should still reply with 404 status. You can check the reply status with your browser’s network developer tool.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Custom Category Pagination shows countless blank (empty) pages’ is closed to new replies.