There are several threads adressing similar problems but unfortunately I'm unable to solve it after hours of research, trial and error. I'm figuring it fits somewhere between Permalink rewrite 404 conflict- WordPress Taxonomies/ CPT and Custom Post Types, Custom Taxonomies and Permalinks or Custom Taxonomy 404 and I'm seriously sorry if it's a duplicate. I just don't get it and it's driving me nuts.
What I have:
- A custom post type "projects", it's archive working fine at example.com/projects
- A custom taxonomy "project_type" that's working sort of fine if I set it's slug to "projektkategorie", resulting in example.com/projektkategorie/digital. This displays only digital projects as I'd expect. However, example.com/projektkategorie results in a 404. Which isn't too bad, because I don't want this page to exist in the first place ...
What I want:
- example.com/projects is supposed to display all projects as it is doing already
- example.com/projects/digital shall display only digital projects
So I'm changing the slug of of my custom taxonomy "project_type" to be "projects" (the same slug as the custom post type). Now WordPress generates exactly the URLs I want to have for the custom taxonomy (i. e. example.com/projects/digital) ... that's where the fun starts:
- example.com/projects still works fine
- example.com/projects/digital results in a 404, instead ...
- my digital projects show up at example.com/digital (which is a regular page named "digital" that's in no way related to my custom stuff. It's just carrying the same name.)
This is the code:
add_action( 'init', 'add_cpt_projects' );
function add_cpt_projects() {
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'query_var' => true,
'rewrite' => array(
'slug' => 'projects',
'with_front' => false
),
'capability_type' => 'post',
'hierarchical' => false,
'has_archive' => true,
'taxonomies' => array( 'project_type' ),
'supports' => $supports
);
register_post_type( 'projects', $args );
}
add_action( 'init', 'add_cpt_project_type' );
function add_cpt_project_type() {
register_taxonomy( 'project_type',
array( 'projects' ),
array(
'hierarchical' => true,
'has_archive' => true,
'public' => true,
'publicly_queryable' => true,
'rewrite' => array(
'slug' => 'projects', // Works if set to 'projektkategorie', resulting in an URL I don't want
'with_front' => false
)
)
);
}