Whenever you enter an URL with two path segments that doesn't exists, WordPress will automatically parse the query as an attachment.
How to reproduce
- Change you permalink structure to
/%postname%/
. - Enter the following path behind your WordPress root URL:
/foo/bar
make sure "foo" isn't a existing taxonomy or post type. - Hook into
pre_get_posts
and dump theglobal $wp_query
. - As you can see from the debugged information, the system is trying to parse the URL as an attachment since it adds
post_type = 'attachment'
to the query and some other attachment related information. - If there is not a post or page with the slug name from the first segment and an attachment with a slug from the second segment than WordPress will return a 404.
NOTE: I'm not sure about the statements I make in step 2 and 5.
Why care?
I found it very annoying to deal with this default behavior and really would like to disable it somehow, if you know how please let me know. It make it impossible to achieve certain permalink structures. Where you probably want to know about as well. I'm trying to achieve a permalink structure where the current post slug is prefix by it's term slug. For example the post type 'example' has post 'bar' which has a relation with a term with the slug 'foo'. Instead of defining the permalink 'example/bar' it should be defined as 'foo/bar'.