Wordpress Custom Type permalink containing Taxonomy slug Wordpress Custom Type permalink containing Taxonomy slug wordpress wordpress

Wordpress Custom Type permalink containing Taxonomy slug


After more searching I managed to create fairly elegant solution using the custom_post_link filter.

Let's say you have a project Custom Type with a client Taxonomy. Add this hook:

function custom_post_link($post_link, $id = 0){  $post = get_post($id);  if(!is_object($post) || $post->post_type != 'project')  {    return $post_link;  }  $client = 'misc';  if($terms = wp_get_object_terms($post->ID, 'client'))  {    $client = $terms[0]->slug;    //Replace the query var surrounded by % with the slug of     //the first taxonomy it belongs to.    return str_replace('%client%', $client, $post_link);  }  //If all else fails, just return the $post_link.  return $post_link;}add_filter('post_type_link', 'custom_post_link', 1, 3);

Then, when registering the Custom Type, set the rewrite arg like this:

'rewrite' => array('slug' => '%client%')

I guess I should have dug deeper before asking, but at least we have a complete solution now.