404 error – How to disable WordPress Rewriting URL with Nearest Matching Page Slug?
WordPress appears to rewrite URLs, displaying Pages under the incorrect URL.
Example.
I have a page called “Emotion” with the slug /emotion/
On the front-end it should appear as the following URL ;
example.com/learning/domains/emotion/
That is, there is a child/parent relationship defined within the Pages section of the website, as shown in that URL.
Unfortunately, wordpress chooses to show anyone the page, no matter what URL you type in
Examples;
- example.com/search/emotion/
- example.com/this-parent-doesnt-exist/emotion/
- example.com/learning/concepts/emotion/
None of these are real paths. I expect to see a 404 page.
I’ve done research already, and tried the following. The first one used to work in WP versions prior to v6. Neither work.
-
remove_filter(‘template_redirect’, ‘redirect_canonical’);
-
add_filter( ‘redirect_canonical’, ‘disable_redirect_canonical’, 10, 2 );
function disable_redirect_canonical( $redirect_url ) {
return false;
}
How do I disable this rewrite rule?
Leave an answer