WordPress.org

Make WordPress Core

Opened 6 years ago

Closed 5 years ago

#30018 closed defect (bug) (fixed)

404 error and redirect when using url with query string

Reported by: pmwp Owned by: wonderboymusic
Milestone: 4.4 Priority: normal
Severity: normal Version: 4.0
Component: Posts, Post Types Keywords: has-patch
Focuses: Cc:

Description

When a query var and the query_var of non-public CPT are the same:

  • You will get a 404 error even when the permalink exists.
  • redirect_guess_404_permalink() returns the permalink of other post which has slug like {query value}%.

Here an example:

  • CPT foo has 'public' => false.
  • The permalink /a123 exists.
  • The permalink /bar/456 exists.

When you visit /a123?foo=4, You will redirect to other url bar/456?foo=4 and get a 404 error.
I want to get /a123 page and $_GET[ 'foo' ] which is not the CTP query_var.

I think CTP query_var parameter must be set to false, if public parameter is false.

Related tickets: #17609, #19693

Attachments (3)

30018.diff (775 bytes) - added by wonderboymusic 5 years ago.
30018.2.diff (2.4 KB) - added by wonderboymusic 5 years ago.
30018.3.diff (2.7 KB) - added by wonderboymusic 5 years ago.

Download all attachments as: .zip

Change History (8)

This ticket was mentioned in Slack in #core by jorbin. View the logs.


6 years ago

#2 @jorbin
6 years ago

@markjaquith - do you think this is canonical related? Is there a change here that might make sense?

@wonderboymusic
5 years ago

#3 @wonderboymusic
5 years ago

  • Keywords has-patch dev-feedback added
  • Milestone changed from Awaiting Review to 4.4

30018.diff fixes it. It's in WP::parse_request() - this seems like a reasonable fix, curious if it would break anything?

#4 @wonderboymusic
5 years ago

  • Keywords dev-feedback removed

In 30018.2.diff, non-viewable post types shouldn't have their query vars parsed in WP::parse_request(). Unit test (surprise, surprise) demonstrates this behavior. If you parse their query vars, and the var does not actually exist to represent that item, the request goes 404. The default value for query_var is true, which means it will be set to $post_type if you don't specify something else. register_post_type() then indiscriminately adds it to WP::public_query_vars

#5 @wonderboymusic
5 years ago

  • Owner set to wonderboymusic
  • Resolution set to fixed
  • Status changed from new to closed

In 34215:

In WP::parse_request(), don't add query vars of non-viewable post types to WP::public_query_vars. In register_post_type(), don't add query vars of non-viewable post types to WP::public_query_vars.
In _unregister_post_type() (unit tests), don't add query vars of non-viewable post types to WP::public_query_vars.

Adds unit test.

Fixes #30018.

Note: See TracTickets for help on using tickets.