As 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.com/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.com/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.com/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.