最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

permalinks - Custom Taxonomy rewrite rule causes 404 error on page or single post depending on if it's set to "

programmeradmin2浏览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/%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
  • Thanks @SallyCJ! My site doesn't have a PAGE with url site/location or any of the locations listed under taxonomy: location (e.g. california, etc.). Now, I can visit example/{location}/{post-slug} without any issues as rewrite rule is tagged as "True", however my Pages - e.g. example/page-slug now all have 404 error. Help? Sorry if I have not understood correctly. Thanks so much for this! – Bacorrito Commented 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/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/%location%/ - see the %location% part. – Sally CJ Commented Jun 20, 2019 at 15:32
  • And when you disable the taxonomy's URL rewriting, then example/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 Commented 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 Commented 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 Commented Jun 20, 2019 at 16:20
Add a comment  | 

1 Answer 1

Reset to default 1

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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论