I've been googling, tinkering around and investigating/testing settings however nothing seems to work so please bear with me on this and please help me if you can. Thanks so much!
I have the custom taxonomy called "location" created via the CPT UI plugin and "Custom Structure" of example/%location%/%postname%/ in Settings >> Permalinks.
Below is the code used by CPT UI plugin to register new taxonomy "location".
function cptui_register_my_taxes_location() {
/**
* Taxonomy: Locations.
*/
$args = array(
"public" => true,
"publicly_queryable" => true,
"hierarchical" => true,
"show_ui" => true,
"show_in_menu" => true,
"show_in_nav_menus" => true,
"query_var" => true,
"rewrite" => array( 'slug' => 'location', 'with_front' => true, ),
"show_admin_column" => true,
"show_in_rest" => true,
"rest_base" => "location",
"rest_controller_class" => "WP_REST_Terms_Controller",
"show_in_quick_edit" => true,
);
register_taxonomy( "location", array( "post", "blog" ), $args );
}
add_action( 'init', 'cptui_register_my_taxes_location' );
So my standard posts have this URL: sitename/"location"/"postname"
However when viewing my standard WP PAGES sitename/contact, sitename/about-us, and all other items under "pages", it returns 404 error.
**Some investigation result:**When I change "rewrite"
to False, issue is fixed on the standard page, however 404 error is encountered in the post e.g. sitename/"location"/"postname". When "rewrite"
is set back to True, single post is with no error however 404 is encountered in Page.
Has anyone encountered this issue before, or does anyone have any idea why this is happening?
Appreciate your help! Much thanks guys!
I've been googling, tinkering around and investigating/testing settings however nothing seems to work so please bear with me on this and please help me if you can. Thanks so much!
I have the custom taxonomy called "location" created via the CPT UI plugin and "Custom Structure" of example/%location%/%postname%/ in Settings >> Permalinks.
Below is the code used by CPT UI plugin to register new taxonomy "location".
function cptui_register_my_taxes_location() {
/**
* Taxonomy: Locations.
*/
$args = array(
"public" => true,
"publicly_queryable" => true,
"hierarchical" => true,
"show_ui" => true,
"show_in_menu" => true,
"show_in_nav_menus" => true,
"query_var" => true,
"rewrite" => array( 'slug' => 'location', 'with_front' => true, ),
"show_admin_column" => true,
"show_in_rest" => true,
"rest_base" => "location",
"rest_controller_class" => "WP_REST_Terms_Controller",
"show_in_quick_edit" => true,
);
register_taxonomy( "location", array( "post", "blog" ), $args );
}
add_action( 'init', 'cptui_register_my_taxes_location' );
So my standard posts have this URL: sitename/"location"/"postname"
However when viewing my standard WP PAGES sitename/contact, sitename/about-us, and all other items under "pages", it returns 404 error.
**Some investigation result:**When I change "rewrite"
to False, issue is fixed on the standard page, however 404 error is encountered in the post e.g. sitename/"location"/"postname". When "rewrite"
is set back to True, single post is with no error however 404 is encountered in Page.
Has anyone encountered this issue before, or does anyone have any idea why this is happening?
Appreciate your help! Much thanks guys!
Share Improve this question asked Jun 20, 2019 at 9:36 BacorritoBacorrito 54 bronze badges 5 |1 Answer
Reset to default 1As pointed in my comments, here are the issues with the /%location%/%postname%/
permalink structure:
When the taxonomy's URL rewriting is enabled, going to a Page like
example/page-slug
would result in a "page not found" error because WordPress sees the request as a "location" request, where in that example, WordPress would try to find a "location" (a term in that taxonomy) where the slug ispage-slug
.When the taxonomy's URL rewriting is disabled, going to a Post like
example/california/post-slug
would result in the same "page not found" error because WordPress sees the request as a child Page request, where WordPress would try to find a Page where the slug iscalifornia/post-slug
.
Now you asked in the comment:
Also I'm just curious, why is it that in Settings > Permalinks I switch instead to
%category%/%post-name%
, issue does not exist? Will wp not also see it as a "category" request - and will try to find a "category" with the slugpage-slug
?
And the answer is, because WordPress automatically turns on "verbose page rules" (or technically, sets WP_Rewrite::$use_verbose_page_rules
to true
) if the permalink structure begins with %category%
(or with a slash — /%category
).
And what does that mean is, you can make your custom permalink structure works properly just like the %category/%post-name%
structure, by enabling the verbose page rules.
How so?
Because that way, URLs matching a Page request would first be checked if it's actually a Page.
Which means, in your case, example/page-slug
for example would be checked if it is a Page before checking if it's a "location"/term.
And here's how can you enable the verbose page rules:
register_taxonomy( "location", array( "post", "blog" ), $args );
$GLOBALS['wp_rewrite']->use_verbose_page_rules = true; // add this
I.e. Add the second line after calling register_taxonomy()
.
Or this works, too:
add_action( 'init', function(){
global $wp_rewrite;
$wp_rewrite->use_verbose_page_rules = true;
}, 1 );
Note: Be sure to flush the permalinks (just visit the Permalink Settings page) after you applied the above code changes.
example/page-slug
, WordPress sees it as a "location" request - and will try to find a "location" with the slugpage-slug
- because of your custom structure -example/%location%/
- see the%location%
part. – Sally CJ Commented Jun 20, 2019 at 15:32example/california/post-slug
for example, defaults to a child Page request - i.e. WordPress will try to find a Page with the slugcalifornia/post-slug
. – Sally CJ Commented Jun 20, 2019 at 16:05