0

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.com/%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.com/"location"/"postname"

However when viewing my standard WP PAGES sitename.com/contact, sitename.com/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.com/"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!

5
  • Thanks @SallyCJ! My site doesn't have a PAGE with url site.com/location or any of the locations listed under taxonomy: location (e.g. california, etc.). Now, I can visit example.com/{location}/{post-slug} without any issues as rewrite rule is tagged as "True", however my Pages - e.g. example.com/page-slug now all have 404 error. Help? Sorry if I have not understood correctly. Thanks so much for this!
    – Bacorrito
    Jun 20, 2019 at 12:53
  • Sorry, please ignore that comment - I thought I've edited it. What I meant is, when you go to example.com/page-slug, WordPress sees it as a "location" request - and will try to find a "location" with the slug page-slug - because of your custom structure - example.com/%location%/ - see the %location% part.
    – Sally CJ
    Jun 20, 2019 at 15:32
  • And when you disable the taxonomy's URL rewriting, then example.com/california/post-slug for example, defaults to a child Page request - i.e. WordPress will try to find a Page with the slug california/post-slug.
    – Sally CJ
    Jun 20, 2019 at 16:05
  • Wow @SallyCJ much thanks for the explanation it's much clearer now... Is a better way to handle this problem? 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 slug page-slug? (i tried and it's ok - no issues at all) Thanks so much for your help as always! :)
    – Bacorrito
    Jun 20, 2019 at 16:18
  • By the way if I ultimately am unable to solve this, I'm just thinking of using the standard "category" taxonomy as the default for my locations... meaning my san francisco, new york, los angeles taxonomies that are supposed to be a custom "location" taxonomy, I'll just put them in the standard "wp" Category to avoid further issues. And then I'll just create a custom taxonomy for the actual categories I have for my posts since they are of lesser importance which don't need to go into the url slug.. What do you think? your thoughts on this are highly valued.. Much thanks @SallyCJ
    – Bacorrito
    Jun 20, 2019 at 16:20

1 Answer 1

0

As pointed in my comments, here are the issues with the /%location%/%postname%/ permalink structure:

  1. 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 is page-slug.

  2. 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 is california/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 slug page-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.

3
  • 1
    OMG @SallyCJ this worked! I used the second code because it's global and I am unable to edit codes by CPT UI plugin. I've been googling around so much for this but I wasn't able to find a solution like this one! I'm sure this will help out many others! Thanks for giving out two solutions too, this will be very helpful as reference in the future :)
    – Bacorrito
    Jun 21, 2019 at 5:28
  • 1
    And @SallyCJ thanks for the explanation too, it all makes perfect sense to me now.. :)
    – Bacorrito
    Jun 21, 2019 at 5:31
  • I'm glad I could help. :)
    – Sally CJ
    Jun 22, 2019 at 10:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.