Make WordPress Core

Opened 4 weeks ago

Closed 4 weeks ago

#63575 closed defect (bug) (duplicate)

Problem with posts pagination

Reported by: szymonmil111's profile szymonmil111 Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: trunk
Component: Permalinks Keywords:
Focuses: Cc:

Description

Hi,

There is not-easy-to-track bug in posts pagination.

Steps to reproduce (on clean installation of WP in 6.8.1 version):

  1. In Permalink settings (/wp-admin/options-permalink.php) set Category base to value X
  2. Set Permalink structure to "/%postname%/"
  3. Create new Page with slug X (the same value as in Permalink settings)
  4. In Reading settings (/wp-admin/options-reading.php) set page X as "Posts page"

Everything will work fine except pagination. Let's assume that the X value is "news". And there is result of what we got when enter specific page:

  • /news = list of all posts (because that page was set as "Posts page"
  • /news/informations = list of posts from category "Informations"
  • /news/page/2 = should recevie second page of all posts, but return 404 error!

I didn't deep-dive into that issue and I didn't figure out what casuses the problem at the code level.

In my opinion, in that situation there should be error when I try to:

  • create new page (or change slug to) with slug which is "Category base" in Permalink settings
  • change "Category base" value to value that is slug of some page

I spent some time to figure out why there is 404 on /page/2 and it is not clear

Change History (1)

#1 @sandeepdahiya
4 weeks ago

  • Resolution set to duplicate
  • Status changed from new to closed

In this case, WordPress uses two queries - 1. main query and 2. custom WP_Query. WordPress uses the main query to decide: which page to load and/or whether to show a 404 error

If you're using custom post types (like news) and you use a separate WP_Query to fetch and paginate those posts—for example, setting posts_per_page to 5 for a total of 20 news posts—you end up with 4 pages in your custom query.

However, if this query runs on a standard WordPress page that doesn’t normally have pagination (such as a regular "news" page created in the admin area), WordPress's main query is unaware of those 4 pages. So when a user navigates to example.com/news/page/2, WordPress checks the main query to see if page 2 exists. Since the main query only knows about page 1, it returns a 404 error—even though your custom WP_Query could technically fetch the correct posts for page 2.

This has been explained better here #16168.

You can always modify the main query using hooks like pre_get_posts instead of creating a new custom query.

Note: See TracTickets for help on using tickets.