How to rewrite permalinks for a custom posttype and custom taxonomy?

I have a custom taxonomy: region, and a custom posttype: business.

Now, how to rewrite the permalink, so that

  • for a category, the fixed "category" part is replaced with the custom taxonomy term:


  • for a business: /%region%/%category%/%business%/

I'm stuck with this. However, I did manage to rewrite the permalink for a region to /%region%/:

add_action( 'init', 'region_init' );
function region_init() {
  // set labels
    $labels = array(
    'name' => _x( 'Regions', 'taxonomy general name' ),
    'singular_name' => _x( 'Region', 'taxonomy singular name' ),
    'search_items' =>  __( 'Search Regions' ),
    'popular_items' =>  __( 'Popular Regions' ),
    'all_items' => __( 'All Regions' ),
    'parent_item' => __( 'Parent Region' ),
    'parent_item_colon' => __( 'Parent Region:' ),
    'edit_item' => __( 'Edit Region' ), 
    'update_item' => __( 'Update Region' ),
    'add_new_item' => __( 'Add New Region' ),
    'new_item_name' => __( 'New Region Name' )
  // create a new taxonomy
      'labels' => $labels,
      'label' => __('Region'),
      'sort' => true,
      'args' => array('orderby' => 'term_order'),
      'show_in_nav_menus' => true,
      'query_var' => true,
      'rewrite' => array( 'slug' => '', 'with_front' => false )

add_action('init', 'my_rewrite');
function my_rewrite() {
    global $wp_rewrite;
    $wp_rewrite->add_permastruct('typename', 'typename/%year%/%postname%/', true, 1);
    add_rewrite_rule('typename/([0-9]{4})/(.+)/?$', 'index.php?typename=$matches[2]', 'top');
    $wp_rewrite->flush_rules(); // !!!
  • 1
    I have a tutorial that should help get you started on more advanced rewrite rules for the post types, vocecommunications.com/blog/2010/11/…. But I haven't yet had a need to try it with a taxonomy included in the permastructure, so there may be some caveats. Commented Dec 17, 2010 at 13:16
  • Is the region taxonomy hierarchical? Because that would mean WordPress can't know how to split /limburg/maastricht/food/pizzeria/mamma-mia/ into a region and a category. Businesses will probably require a unique slug, even if they are in separate regions, so you can't have /rotterdam/pizzeria/mamma-mia/ and /amsterdam/pizzeria/mamma-mia/, one of them will need to be /mamma-mia-2/. This could be the key to solve this problem: ignore the region and category, do the lookup only on the last part.
    – Jan Fabry
    Commented Dec 28, 2010 at 11:54
  • @Jan: the region taxonomy is non-hierarchical. Interesting remark about slug uniqueness. Would it be possible then to rewrite the business name, so that it includes the region. So, e.g. /amsterdam-mamma-mia/
    – Henk Jan
    Commented Dec 29, 2010 at 11:18
  • Indeed, you can define the slug of each post yourself. Does this mean you leave out the region, to get /pizzeria/amsterdam-mamma-mia/ (which is actually just /%category%/%postname%/), or duplicate it, like /amsterdam/pizzeria/amsterdam-mamma-mia/? (But... with some regex-fu we can even use /amsterdam/pizzeria/mamma-mia/ and still have the actual slug be amsterdam-mamma-mia.) If you can come to the chat we can try to figure this out together.
    – Jan Fabry
    Commented Dec 29, 2010 at 12:47

1 Answer 1


The Custom Post Permalinks (Wordpress Plugin) from John P. Bloch did the trick for me. Great /flexible plugin!

Your Answer

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

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