Custom Permalinks for Custom post Type Archives?

Take an example custom post type called movie the code below will create custom permalinks awesomemovies/ regardless of the one created by using the has_archive and rewrite options of register_post_type.

add_filter( 'rewrite_rules_array', 'custom_permalink_for_my_cpt' );
function custom_permalink_for_my_cpt( $rules ) {
    $custom_rules = array();
    // for archive urls
    $custom_rules['awesomemovies/?$'] = 'index.php?post_type=movie';

    // for individual post urls e.g: http://blog.com/awesomemovies/post-name/
    $custom_rules['awesomemovies/([^/]+)/?$'] = 'index.php?post_type=movie&pagename=$matches[1]';
    return $custom_rules + $rules;
}

For more information see WP_Rewrite

Leave a Comment