0

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
      )
    )
  );
}
  • can you please try to register your tax BEFORE the cpt. that is required for when u want to use the tax hierarchy inside the cpt url. btw. that init part is actually missing in ur code above, please add that, too.. and do you save your permalink settings, when u change ur code..?! – honk31 Jun 12 '18 at 8:03
  • @honk31 Registering the taxonomy before the custom post type actually did the trick, thank you! I already suspected it to be something as simple as that while wasting hours on this. But maybe you should post that as an answer instead of a comment to earn the credit you deserve? – Nico Jun 12 '18 at 8:35
1

When you want to use a taxonomy inside a custom post type URL hierarchy, you have to register it BEFORE the custom post type. Otherwise it won't work properly. here is a short example:

function so305901_register_taxonomies() {
    $args = [
        //your settings
    ];
    register_taxonomy( 'taxonomy_slug', ['custom_post_type_slug'], $args );
}
add_action( 'init', 'so305901_register_taxonomies' );

function so305901_register_custom_post_types() {
    $args = [
        //your settings
        'taxonomies'    => [
            'taxonomy_slug'
        ],
    ];
    register_post_type( 'custom_post_type_slug', $args );
}
add_action( 'init', 'so305901_register_custom_post_types' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.