admin管理员组

文章数量:1279247

So, I've created a CPT - Community Post:

For this CPT I've created two custom taxonomies:

community_post_domain community_post_type My goal is that the permalink of all single community posts will go as follows: /%POST_DOMAIN_SLUG%/%POST_SLUG% So, for example, if I have a community post with the title 'Go Micro Services', and it has community_post_domain set to a term named Kubernetes, the permalink will be: .

This is how I do the permalink manipulation:

Also, I've created a static page: Community, that has the permalink: . So far, so good. The problem arises when I try to publish a page and I want it to have a permalink like so: /%PAGE_SLUG% I create the page, let's say, for example, Hello World. I set the parent to be the Community page. When I click Publish, the page is created, and in the WordPress dashboard I see that the page's permalink is as expected: BUT, and this is the actual problem - when I'm trying to visit the page's permalink, I get 404. I tried going to Permalinks in WP dashboard and update the permalinks, didn't help. Any Ideas here?

So, I've created a CPT - Community Post: https://pastebin/9XSrvZAr

For this CPT I've created two custom taxonomies: https://pastebin/fNhfpiM6

community_post_domain community_post_type My goal is that the permalink of all single community posts will go as follows: https://example/community/%POST_DOMAIN_SLUG%/%POST_SLUG% So, for example, if I have a community post with the title 'Go Micro Services', and it has community_post_domain set to a term named Kubernetes, the permalink will be: https://example/community/kubernetes/go-micro-services.

This is how I do the permalink manipulation: https://pastebin/aP5rEBjY

Also, I've created a static page: Community, that has the permalink: https://example/community. So far, so good. The problem arises when I try to publish a page and I want it to have a permalink like so: https://example/community/%PAGE_SLUG% I create the page, let's say, for example, Hello World. I set the parent to be the Community page. When I click Publish, the page is created, and in the WordPress dashboard I see that the page's permalink is as expected: https://example/community/hello-world BUT, and this is the actual problem - when I'm trying to visit the page's permalink, I get 404. I tried going to Permalinks in WP dashboard and update the permalinks, didn't help. Any Ideas here?

Share Improve this question asked Sep 26, 2021 at 9:52 NadavNadav 1011 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 1

Did a quick test on my local dev - https://pastebin/bP8byGHR - look for the comments // here.

On your taxonomies I had to remove capabilities because it wasn't showing on my menu, ignore if it works for you but I don't see any use case for it.

Your rewrites were the issue, you can fix the permalinks structure with post_type_link:

add_filter('post_type_link', 'so_update_permalink_structure', 10, 2);

function so_update_permalink_structure( $post_link, $post )
{
    if ( false !== strpos( $post_link, '%community_post_domain%' ) ) {
        $taxonomy_terms = get_the_terms( $post->ID, 'community_post_domain' );
        foreach ( $taxonomy_terms as $term ) {
            if ( ! $term->parent ) {
                $post_link = str_replace( '%community_post_domain%', $term->slug, $post_link );
            }
        }
    }
    return $post_link;
}

Don't forget to flush permalinks and test.

With a quick search I've found a blog post that explains much better than me and it can be useful for your case: https://react2wp/using-same-slug-for-custom-post-type-and-custom-taxonomy/

edit: My permalinks tests:


// Created a page with the same slug
// Test url: ok
url.test/community/

// Created the taxonomy 'kubernetes'
// Test url: ok
url.test/community/kubernetes/

// Created a new cpt post
// Post with a taxonomy: ok
url.test/community/kubernetes/go-micro-services/

// Post without a taxonomy: error 400
// For this case you should set a default taxonomy to prevent an error
url.test/community/%community_post_domain%/go-micro-services/


本文标签: Custom Permalinks For CPT and pages with parent Advanced Wordpress