0

I am trying to get a custom permalink style for my custom post types... my current permalink for my post is /%postname%/ but for my custom post types, I am trying to get the custom taxonomy in the permalink. I don't actually want the custom taxonomy name IN the permalink, but instead, let's say the custom taxonomy name is "TYPES" and under it I create "PRODUCTS" and my custom post type is "REVIEWS"...

I want a permalink structure that is like /REVIEWS/PRODUCTS/%POSTNAME%

From my current permalink structure, it just shows /REVIEWS/%POSTNAME%. Any idea how I can accomplish the above? BTW I am using Custom Post Type UI plugin to create the custom post type and taxonomy. Also tried using the Custom Post Type Permalinks plugin... but settings %taxonomy% doesn't work, I am guessing I have to register that somewhere.

1
  • I just did something very similar and I used this method to do it. Cheers
    – David
    Apr 7, 2012 at 5:58

2 Answers 2

0

This code will do what you want but it assumes yoru taxonomy and post type have no heirarchy, e.g. your taxonomy works like tags not categories, and your post type cannot have parent and child posts.

This code belongs in a plugin or the functions.php of your theme:

function custom_rewrite( $wp_rewrite ) {
    $feed_rules = array(
        'reviews/(.+)/(.+)'      =>  'index.php?post_type=reviews&taxonomy_name='.$wp_rewrite->preg_index(1).'&post_name=' . $wp_rewrite->preg_index(2)
    );
    $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' );

Once the code has been inserted, go to the permalinks settings page and re-save to flush the old permalink rules and generate the new ones.

If your taxonomy or your post type IS heirarchical however, it can still be done, but you will need to modify the regular expression in the code accordingly. I am not familiar enough with regex to advise how.

0

You can take the help from this article.

You will find more information in rewrite attribute description of the article.

Ex.

add_action( 'init', 'create_my_post_types' );
function create_my_post_types() 
{
        register_post_type( 'super_duper',
                array(
                        'labels' => array
                        (
                                'name' => __( 'Super Dupers' ),
                                'singular_name' => __( 'Super Duper' ),
                                'add_new' => __( 'Add New' ),
                                'add_new_item' => __( 'Add New Super Duper' ),
                                'edit' => __( 'Edit' ),
                                'edit_item' => __( 'Edit Super Duper' ),
                                'new_item' => __( 'New Super Duper' ),
                                'view' => __( 'View Super Duper' ),
                                'view_item' => __( 'View Super Duper' ),
                                'search_items' => __( 'Search Super Dupers' ),
                                'not_found' => __( 'No super dupers found' ),
                                'not_found_in_trash' => __( 'No super dupers found in Trash' ),
                                'parent' => __( 'Parent Super Duper' ),

                        ),
                        'public' => true,
                        'show_ui' => true,
                        'publicly_queryable' => true,
                        'exclude_from_search' => false,
                        'rewrite' => array( slug => 'New_label','with_front' => true ),
                )       
        );
}

Provide the slug which you are using in rewrite parameter's slug value to get the desired permalink.

Your Answer

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

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