I was wondering if you could help me with the following issue. I have read a lot of cases, but couldn't trace anywhere good answer which explain, how this works.

I created a custom post type: project and taxonomy: projecten_category I am was able get it working with the project assigned to one category. I have created project 1 and assigned it to category1. I have created project 2 and assigned it to category2. I have created project 3 and assigned it to category1 - category2.

When I created project 3 in the dashboard it get the permalink projecten/category1/project like a default.

But I would like to have it like this: projecten/category1/project1 (working if is one category) projecten/category2/project2 (working if is one category)

Project three get automatically: projecten/category1/project3

Is there a option to get for project 3 a url projecten/category1/project3 if it comes from the category1 page and projecten/category2/project3 if it comes from category 2 page. and can I create a kind of default for projects assigned to multiple categories?

This is my code:

// Register Custom Post Type for Projects
function custom_post_type_project() {
    $labels = array(
        'name'               => _x('Projects', 'post type general name'),
        'singular_name'      => _x('Project', 'post type singular name'),
        'menu_name'          => _x('Projects', 'admin menu'),
        'add_new'            => _x('Add New', 'project'),
        'add_new_item'       => __('Add New Project'),
        'new_item'           => __('New Project'),
        'edit_item'          => __('Edit Project'),
        'view_item'          => __('View Project'),
        'all_items'          => __('All Projects'),
        'search_items'       => __('Search Projects'),
        'not_found'          => __('No projects found'),
        'not_found_in_trash' => __('No projects found in Trash'),

    $args = array(
        'labels'             => $labels,
        'public'             => true,
        'publicly_queryable' => true,
        'show_ui'            => true,
        'show_in_menu'       => true,
        'query_var'          => true,
        'rewrite'            => array('slug' => 'projecten/%projecten_category%', 'with_front' => false), // Adjust slug
        'capability_type'    => 'post',
        'has_archive'        => 'projecten', // Adjust archive slug
        'hierarchical'       => false,
        'menu_position'      => null,
        'supports'           => array('title', 'editor', 'thumbnail'),
        'taxonomies'         => array('projecten_category'), // Assigning taxonomy to the custom post type

    register_post_type('project', $args);
add_action('init', 'custom_post_type_project');

// Register Custom Taxonomy for Projecten Categories
function custom_taxonomy_projecten_category() {
    $labels = array(
        'name'              => _x('Projecten Categories', 'taxonomy general name'),
        'singular_name'     => _x('Projecten Category', 'taxonomy singular name'),
        'search_items'      => __('Search Projecten Categories'),
        'all_items'         => __('All Projecten Categories'),
        'parent_item'       => __('Parent Projecten Category'),
        'parent_item_colon' => __('Parent Projecten Category:'),
        'edit_item'         => __('Edit Projecten Category'),
        'update_item'       => __('Update Projecten Category'),
        'add_new_item'      => __('Add New Projecten Category'),
        'new_item_name'     => __('New Projecten Category Name'),
        'menu_name'         => __('Projecten Categories'),

    $args = array(
        'hierarchical'      => true,
        'labels'            => $labels,
        'rewrite'           => array('slug' => 'projecten'), // Adjust slug

    register_taxonomy('projecten_category', 'project', $args); // Associating with 'project' post type
add_action('init', 'custom_taxonomy_projecten_category');

 * Replace projecten category slug in project post type permalink.
 * @param  string  $link
 * @param  WP_Post $post 
 * @return string
function replace_projecten_category_slug( $link, $post ) {
    if ( $post->post_type === 'project' ) {
        $terms = get_the_terms( $post->ID, 'projecten_category' );
        if ( !empty($terms) ) {
            $categories = wp_list_pluck( $terms, 'slug' );
            $category_slug = $categories[0]; // Get the first category slug
            $link = str_replace( '%projecten_category%', $category_slug, $link );

    return $link;
add_filter( 'post_type_link', 'replace_projecten_category_slug', 10, 2 );


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.