How to remove custom post type slug from url - wordpress
Wednesday, November 25, 2015
There are times that you want to get rid of from the regular permalink structure that WordPress applies to your custom post types. For a recent project ie HaircutMatch.com I was needing to remove the CPT slug 'store' from the permalink, so a page url like “/store/some-profile-name/” would have the permalink of “/some-profile-name/.” 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” isn't it? & none of which work for you as exaclty same as you want. Then there are time you find a script that just works, and that is what I am sharing with you.
Just paste the following code in your functions.php file and replace 'store' & “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 bilalhssn_remove_cpt_slug( $post_link, $post, $leavename ) {
if ( ! in_array( $post->post_type, array( 'store' ) ) || 'publish' != $post->post_status )
return $post_link;
$post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );
return $post_link;
}
add_filter( 'post_type_link', 'bilalhssn_remove_cpt_slug', 10, 3 );
function bilalhssn_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', 'store', 'page' ) );
}
add_action( 'pre_get_posts', 'bilalhssn_parse_request_tricksy' );
Just paste the following code in your functions.php file and replace 'store' & “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 bilalhssn_remove_cpt_slug( $post_link, $post, $leavename ) {
if ( ! in_array( $post->post_type, array( 'store' ) ) || 'publish' != $post->post_status )
return $post_link;
$post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );
return $post_link;
}
add_filter( 'post_type_link', 'bilalhssn_remove_cpt_slug', 10, 3 );
function bilalhssn_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', 'store', 'page' ) );
}
add_action( 'pre_get_posts', 'bilalhssn_parse_request_tricksy' );

0 comments