custom post types - Using taxonomy term in CPT permalink – Pages 404's

admin2025-06-02  1

Having some issues with creating the permalink structure that I'd like. Found a piece of code on Stack Exchange, which unfortunately I cannot locate right now.

I have a CPT called project and I want to be able to achieve this structure /taxonomy/title-of-project

This is working at the moment, using this code

/* Initialize Project CPT **/
add_action( 'init', function() {
  $rewrite = array(
    'slug'                  => '%department%',
    'with_front'            => true
  );
  $args = array(
    'label'                 => __( 'Project', 'sage' ),
    'labels'                => $labels,
    'supports'              => array( 'title', 'thumbnail'),
    'hierarchical'          => false,
    'public'                => true,
    'show_ui'               => true,
    'show_in_menu'          => true,
    'menu_position'         => 5,
    'show_in_admin_bar'     => false,
    'show_in_nav_menus'     => true,
    'can_export'            => true,
    'has_archive'           => false,    
    'exclude_from_search'   => false,
    'publicly_queryable'    => true,
    'rewrite'               => $rewrite,
    'capability_type'       => 'post',
    'show_in_rest'          => true,
  );
  register_post_type( 'project', $args );
}, 0);


/* Department Taxonomy **/
add_action( 'init', function () {
    register_taxonomy('department', array('project'), array(
        'hierarchical' => true,
        'labels' => $labels,
        'show_ui' => true,
        'show_admin_column' => true,
        'query_var' => true,
        'rewrite' => array( 'with_front' => false),
    ));
}, 0 );


/* Filter post_type_link to insert the taxonomy term into the permalink **/
add_filter('post_type_link', function ( $post_link, $id = 0 ) {
    $post = get_post($id);  
    if (is_object( $post ) && $post->post_type == 'project' ) {
        $terms = wp_get_object_terms( $post->ID, 'department' );
        if($terms) {
            return str_replace( '%department%' , $terms[0]->slug , $post_link );
        }
    }
    return $post_link;  
}, 1, 3);

But, when adding this code, all my pages will 404. If I comment out line 4:
//'slug' => '%department%',

Then it works just fine. I guess it's a permalink collision somehow, but not sure how I'd go about to fix it. Would be super happy if someone could point me in the right direction.

Having some issues with creating the permalink structure that I'd like. Found a piece of code on Stack Exchange, which unfortunately I cannot locate right now.

I have a CPT called project and I want to be able to achieve this structure https://mysite.tld/taxonomy/title-of-project

This is working at the moment, using this code

/* Initialize Project CPT **/
add_action( 'init', function() {
  $rewrite = array(
    'slug'                  => '%department%',
    'with_front'            => true
  );
  $args = array(
    'label'                 => __( 'Project', 'sage' ),
    'labels'                => $labels,
    'supports'              => array( 'title', 'thumbnail'),
    'hierarchical'          => false,
    'public'                => true,
    'show_ui'               => true,
    'show_in_menu'          => true,
    'menu_position'         => 5,
    'show_in_admin_bar'     => false,
    'show_in_nav_menus'     => true,
    'can_export'            => true,
    'has_archive'           => false,    
    'exclude_from_search'   => false,
    'publicly_queryable'    => true,
    'rewrite'               => $rewrite,
    'capability_type'       => 'post',
    'show_in_rest'          => true,
  );
  register_post_type( 'project', $args );
}, 0);


/* Department Taxonomy **/
add_action( 'init', function () {
    register_taxonomy('department', array('project'), array(
        'hierarchical' => true,
        'labels' => $labels,
        'show_ui' => true,
        'show_admin_column' => true,
        'query_var' => true,
        'rewrite' => array( 'with_front' => false),
    ));
}, 0 );


/* Filter post_type_link to insert the taxonomy term into the permalink **/
add_filter('post_type_link', function ( $post_link, $id = 0 ) {
    $post = get_post($id);  
    if (is_object( $post ) && $post->post_type == 'project' ) {
        $terms = wp_get_object_terms( $post->ID, 'department' );
        if($terms) {
            return str_replace( '%department%' , $terms[0]->slug , $post_link );
        }
    }
    return $post_link;  
}, 1, 3);

But, when adding this code, all my pages will 404. If I comment out line 4:
//'slug' => '%department%',

Then it works just fine. I guess it's a permalink collision somehow, but not sure how I'd go about to fix it. Would be super happy if someone could point me in the right direction.

Share Improve this question edited Mar 9, 2019 at 19:31 INT asked Mar 9, 2019 at 19:25 INTINT 1,2813 gold badges21 silver badges51 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Ended up with commenting out line 4 and changing the filter into this:

add_filter('post_type_link', function ( $post_link, $post, $id = 0 ) {
    if ( 'project' != $post->post_type || 'publish' != $post->post_status ) {
        return $post_link;
    }

    $post = get_post($id);  
    if (is_object( $post ) && $post->post_type == 'project' ) {
        $terms = wp_get_object_terms( $post->ID, 'department' );
        if($terms) {
            return str_replace( 'project' , $terms[0]->slug , $post_link );
        }
    }
    return $post_link;  
}, 1, 3);

Now pages works like they should again. All is well!

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748825718a314038.html

最新回复(0)