Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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:

    /%region%/%category%/

  • 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
  register_taxonomy(
    'region',
    'business',
    array(
      '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(); // !!!
}
share|improve this question
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. –  prettyboymp Dec 17 '10 at 13:16
    
Thanks. I will have a look at the tutorial. –  Henk Jan Dec 17 '10 at 14:29
    
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 Dec 28 '10 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 Dec 29 '10 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 Dec 29 '10 at 12:47
add comment

1 Answer

up vote 1 down vote accepted

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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