Support » Developing with WordPress » Permalink for CPT without taxonomy

  • Resolved imconfused4sure

    (@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 );
Viewing 1 replies (of 1 total)
  • Thread Starter imconfused4sure

    (@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);
Viewing 1 replies (of 1 total)
  • You must be logged in to reply to this topic.