• Resolved Carl Brubaker

    (@imconfused4sure)


    I have a CPT with custom taxonomies. I have a page or 2 that are not assigned a custom taxonomy. How can I get those permalinks to work?

    examples:

    //This works
    base_url/post-type/section/has-section-taxonomy/
    
    // How can I get this to work?
    base_url/post-type/has-no-section-taxonomy/

    Register CPT excerpt:

    'rewrite' => ['slug' => 'post-type/%sections%']
    
    register_post_type('post-type', $args);

    Register Taxonomy excerpt:

    $post_types = ['post-type'];
    
    register_taxonomy('sections', $post_types, $args);
    function permalinks( $post_link, $post ){
        if ( is_object( $post ) && $post->post_type == 'post-type' ){
            $terms = wp_get_object_terms( $post->ID, 'sections' );
            if( $terms ){
                return str_replace( '%sections%' , $terms[0]->slug , $post_link );
            }
        }
        return $post_link;
    }
    
    add_filter( 'post_type_link', 'permalinks', 1, 2 );
    • This topic was modified 3 years, 9 months ago by Carl Brubaker.
Viewing 1 replies (of 1 total)
  • Thread Starter Carl Brubaker

    (@imconfused4sure)

    I got it figured out by modifying the code above to include this:

    Register CPT excerpt:

    'rewrite' => ['slug' => 'post-type/%sections%']
    
    register_post_type('post-type', $args);

    Register Taxonomy excerpt:

    'rewrite' => ['slug' => 'post-type']
    
    $post_types = ['post-type'];
    
    register_taxonomy('sections', $post_types, $args);
    • This reply was modified 3 years, 9 months ago by Carl Brubaker.
Viewing 1 replies (of 1 total)
  • The topic ‘Permalink for CPT without taxonomy’ is closed to new replies.