There are times that you want to stray from the regular permalink structure that WordPress applies to your custom post types. For a recent project I was needing to remove the CPT slug from the permalink, so a page url like “/events/some-event-slug/” would have the permalink of “/some-event-slug/.” At first glance this doesn’t seem to be that difficult to accomplish, but then you start looking through the WordPress.org forums for a solution and come back with several different “solutions” and none of which work for you. Then there are time you find a script that just works, and that is what I am sharing with you.
Thanks WordPress VIP for posting a solution.
Just paste the following code in your functions.php file and replace “your_post_type” with the slug of the post type you wish to drop the slug from.
/** * Remove the slug from published post permalinks. Only affect our CPT though. */ function vipx_remove_cpt_slug( $post_link, $post, $leavename ) { if ( ! in_array( $post->post_type, array( 'event' ) ) || 'publish' != $post->post_status ) return $post_link; $post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link ); return $post_link; } add_filter( 'post_type_link', 'vipx_remove_cpt_slug', 10, 3 ); function vipx_parse_request_tricksy( $query ) { // Only noop the main query if ( ! $query->is_main_query() ) return; // Only noop our very specific rewrite rule match if ( 2 != count( $query->query ) || ! isset( $query->query['page'] ) ) return; // 'name' will be set if post permalinks are just post_name, otherwise the page rule will match if ( ! empty( $query->query['name'] ) ) $query->set( 'post_type', array( 'post', 'your_post_type', 'page' ) ); } add_action( 'pre_get_posts', 'vipx_parse_request_tricksy' );
When that’s done be sure and update permalinks. That’s it.
See original post here: http://vip.wordpress.com/documentation/remove-the-slug-from-your-custom-post-type-permalinks/