Make WordPress Core

Opened 4 years ago

Last modified 2 months ago

#50873 new defect (bug)

Search query parameter causes 404 on pagename match rules

Reported by: mikengarrett's profile MikeNGarrett Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 5.4.2
Component: Query Keywords: has-patch has-unit-tests
Focuses: performance Cc:


With permalinks turned on, visit a post or page. Then append ?s=test to the url. It should look like this: You will probably hit the 404 page.

The expected behavior is to get to the post or page you hit, ie

Instead, you hit a 404 page.

This is due to both the search query and post slug query getting combined instead of one taking priority over the other.

The resulting query is a combination of a search query and post slug query which results in no posts being found, unless the search term is also in the post slug. See below:

SELECT wp_posts.*
FROM wp_posts
AND wp_posts.post_name = 'post-slug'
AND (((wp_posts.post_title LIKE '%test%')
OR (wp_posts.post_excerpt LIKE '%test%')
OR (wp_posts.post_content LIKE '%test%')))
AND wp_posts.post_type = 'post'
ORDER BY wp_posts.post_title LIKE '%test%' DESC, wp_posts.post_date DESC

This is using as the example url.

I would expect the search query string to be ignore unless the visitor is on the search page (front/home page by default).

Attachments (1)

50873-search-query.diff (538 bytes) - added by daxelrod 4 years ago.
Checks if the request is a search query before parsing the 's' query var.

Download all attachments as: .zip

Change History (5)

4 years ago

Checks if the request is a search query before parsing the 's' query var.

#2 @daxelrod
4 years ago

In discussing with @MikeNGarrett and @kshaner the 404 behavior originates from class-wp.php:handle_404() which receives a query with no posts.

Tracing backwards, in class-wp-query.php:parse_query() the is_search state should only be triggered if attachment/p/postname/pagename is not set, but class-wp-query.php:get_posts() only looks for strlen( $q['s'] ) (ignoring if it were a singular query). The logic was resulting in a query that was both selecting a post, and searching at the same time. The query (typically) selects 0 posts, which results in the 404.

This ticket was mentioned in PR #465 on WordPress/wordpress-develop by donmhico.

4 years ago

  • Keywords has-patch has-unit-tests added

This PR fixes the issue where 404 is returned when search query parameter is present and a post slug has a match. The PR uses @daxelrod's patch. I added a unit test.

Trac ticket:

ak1051210 commented on PR #465:

2 months ago

The issue occurs when appending "?s=test" to a WordPress post/page URL with permalinks enabled, resulting in a 404 error. This happens because WordPress interprets the entire URL, including the search query, as a post/page slug. Solutions include customizing search behavior, creating a custom search template, setting up redirects, or using plugins to handle URL parsing and search queries more effectively.

Note: See TracTickets for help on using tickets.