$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>custom post types - Using taxonomy term in CPT permalink – Pages 404's|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin8PV0评论

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!

Post a comment

comment list (0)

  1. No comments so far