WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a custom post type called "menu" with the following categories

  • hotboxes
  • salads
  • soups
  • wraps


        "hierarchical" => true, 
        "label" => "Menu Categories", 
        "singular_label" => "Menu Category", 
        "rewrite" => array( 'slug' => 'menu/type', 'hierarchical'=>'true'), 
        "show_ui"=>"radio", // used a plugin
        "query_var" => true

Right now if I want to say, show all my "hotboxes" it goes to


But if it shows an individual hotbox it just goes to


Is there a way to rewrite the url so it says


The ideal format I am looking for is:


EDIT: I managed to get the link looking like the way I wanted by doing:

function menu_post_link( $post_link, $id = 0 ) {

        $post = get_post($id);

        if ( $post->post_type != 'menu')
            return $post_link;  

        if( $terms = wp_get_object_terms( $post->ID, 'menutype' ) ) {
            $custom_taxonomy = $terms[0]->slug;
            $post_link = str_replace('menu', 'menu/'.$custom_taxonomy, $post_link);

        return $post_link;

    add_filter( 'post_type_link', 'menu_post_link', 10, 2 );

Note: My custom post type is called "menu" and my custom taxonomy is called "menutype".

This now creates the permalink I want:


However it generates a 404 error even after flushing my rewrite rules (Settings > Permalinks > save)

share|improve this question
Thanks for fixing my formatting – redconservatory Mar 29 '12 at 21:13
up vote 1 down vote accepted

Try something similar to this:

function custom_rewrite( $wp_rewrite ) {
    $feed_rules = array(
        '(.+)/([^/]+)(/[0-9]+)?/?$'    =>  'index.php?post_type='.$wp_rewrite->preg_index(1).'&custom_taxonomy='. $wp_rewrite->preg_index(2).'&post_name='. $wp_rewrite->preg_index(3)

    $wp_rewrite->rules = $feed_rules + $wp_rewrite->rules;
// refresh/flush permalinks in the dashboard if this is changed in any way
add_filter( 'generate_rewrite_rules', 'custom_rewrite' );

You may need to modify the regex, as regex isn't one of my strongpoints, and you'll need to swap out your taxonomy name in the URL. You may want to hardcode the custom post type too and adjust the rest accordingly to avoid clashes.

share|improve this answer

Your Answer


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.