admin管理员组文章数量:1289732
I have a custom post type called book where I set 'has_archive' => false
. When I access mywebsite/book, it's loading mywebsite/book/book-1, but I'm expecting load the 404 page. Is this a WordPress feature? How can I disable/change this?
I have a custom post type called book where I set 'has_archive' => false
. When I access mywebsite/book, it's loading mywebsite/book/book-1, but I'm expecting load the 404 page. Is this a WordPress feature? How can I disable/change this?
1 Answer
Reset to default 2Is this a WordPress feature?
Yes, and the redirect is being done by redirect_canonical()
, but the URL is determined by redirect_guess_404_permalink()
:
redirect_guess_404_permalink()
Attempts to guess the correct URL for a 404 request based on query vars.
So what's happening in your case is, WordPress attempts to find a post having book
in the slug (post_name
), and then if found, WordPress loads that post instead of showing a 404 error page.
How can I disable/change this?
Completely disable
redirect_canonical()
(which WordPress core hooks ontemplate_redirect
) if the current URL path is exactlybook
as inexample/book/
andexample/book?foo=bar
.So for example, you can use the
parse_request
hook to disable the canonical redirect:add_action( 'parse_request', 'wpse_392546' ); function wpse_392546( $wp ) { if ( 'book' === $wp->request ) { remove_action( 'template_redirect', 'redirect_canonical' ); } }
Or you can use
do_redirect_guess_404_permalink
to disable just the attempt of guessing the correct URL as mentioned above.So you could use the same hook and function as used in option 1 above, but replace the
remove_action()
line with:add_filter( 'do_redirect_guess_404_permalink', '__return_false' );
本文标签: phpCustom Post Type without an archive page
版权声明:本文标题:php - Custom Post Type without an archive page 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741417632a2377611.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论