1

I have a custom taxonomy and a custom post type. I'm aiming for the following permalink structure:

{custom taxonomy}/{custom taxonomy term}/{custom post slug}

Or, at the very least,

{custom taxonomy term}/{custom post slug}

I currently have

{%category%}/{%postname%}, but no dice.

For example, assume I have:

  • a taxonomy states, and a taxonomy term newyork
  • a custom post type jobs with a post that has the slug wordpress-developer

I am looking to achieve:

  • states/newyork: an archive of all the jobs in newyork, this works.
  • states/newyork/wordpress-developer: the custom post's page using the taxonomy & term as the base in the permalink.

The goal is to navigate from states/newyork (list of "jobs" and flow into the individual post w/o deviating from the permalink structure thus far, so states/newyork/wordpress-developer

Problems encountered:

  • states/newyork/wordpress yields a 404
  • newyork/wordpress-developer redirects to jobs/wordpress-developer

Desired outcome:

  • states/newyork/wordpress-developer landing on the custom post type page

PS - not building another job board, just using this as an example :)


current taxonomy & post type register hook:

function register() {
  register_taxonomy('state', 'district', array(
    'labels' => array(
      'name' => 'States',
      'singular_name' => 'State',
      'search_items' => 'Search States',
      'all_items' => 'All States',
      'parent_item' => 'Parent State',
      'parent_item_colon' => 'Parent State:',
      'edit_item' => 'Edit State',
      'update_item' => 'Update State',
      'add_new_item' => 'Add New State',
      'new_item_name' => 'New State',
      'menu_name' => 'States'
    ),
    'public' => true,
    'show_admin_column' => true,
    'hierarchical' => true,
    'query_var' => true,
    'rewrite' => array(
      'slug' => 'states',
      'with_front' => false
    )
  ));

  register_post_type('district',
    array(
      'labels' => array(
        'name' => 'Districts',
        'singular_name' => 'Districts'
      ),
      'hierarchical' => true,
      'supports' => array('title', 'page-attributes'),
      'public' => true,
      'has_archive' => 'states',
      'rewrite' => array(
        'with_front' => false,
        'slug' => 'states/%show_category%'
      )
    )
  );
}
  • 1
    possible duplicate of Permalinks: custom post type -> custom taxonomy -> post – Milo Aug 23 '15 at 17:32
  • @Milo I had tried that, looked very promising, but it resulted in a 404 – Atticus Aug 23 '15 at 17:43
  • The order that you register things is important. Add the code you tried to your question. Also note that your posts permalink structure is irrelevant, that's only the native post type. – Milo Aug 23 '15 at 18:12
  • Updated @Milo. I have the taxonomy, then the post type. I have also tried this in the opposite order – Atticus Aug 24 '15 at 0:23
  • A big red flag was looking at the $wp_query object on the 404 template. The request property appears to not identify the custom post type: SELECT wp_posts.* FROM wp_posts WHERE 1=1 AND wp_posts.post_name = 'district-1' AND wp_posts.post_type = 'post' ORDER BY wp_posts.post_date DESC – Atticus Aug 24 '15 at 0:26
1

I was able to solve this by defining a rewrite rule:

function rewrite_rules($rules) {
  $newRules = array();
  $newRules['states/(.+)/(.+?)$'] = 'index.php?district=$matches[2]';
  return array_merge($newRules, $rules);
}
add_filter('rewrite_rules_array', __NAMESPACE__ . '\\rewrite_rules');

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.