0

Remove The Slugs from Custom Post Type URL

If you are a WordPress user and want to customize the permalink for custom post type to remove the generated slug by WordPress (post_name value) for your custom post type, then you might stumbled across different solution on the web trying something that might achieve the same effect without any performance impact. This method is worth to try.

Note: This is a version we found useful and it worked for a particular case. It may not work for you, but you can post a comment below and maybe we can figure it out.

So, let us get down to the nitty-gritty. All you need are the following lines of code, that should be placed in your theme’s functions.php file, and your permalink structure should be /%postname%/.

First, register your own custom post type normally.
[code language=”php”]
/**
* Register a custom post type
*/
$args = array(
‘description’ => ‘Photograph Post Type’,
‘label’ => __(‘Photographs’),
‘public’ => true,
‘rewrite’ => array( ‘slug’ => ‘photograph’),
);
register_post_type( ‘photograph’ , $args );
[/code]
Then the second thing that we have to do is to remove the actual slug from the actual URL, we want to filter the permalink for our custom post type (in this case is “photograph”) such that all published posts don’t have the slug in the URI.

We can do this by checking the post type and if the current post has the desired type we proceed in removing the slug using str_replace on the permalink. In order for this function to work we need it to be attach to a hook. Luckily, WordPress has a filter called post_type_link that is used.
[code language=”php”]
/**
* Remove the slug from published post permalinks.
*/
function custom_remove_cpt_slug( $post_link, $post, $leavename ) {

if ( ‘photograph’ != $post->post_type || ‘publish’ != $post->post_status ) {
return $post_link;
}

$post_link = str_replace( ‘/’ . $post->post_type . ‘/’, ‘/’, $post_link );

return $post_link;
}
add_filter( ‘post_type_link’, ‘custom_remove_cpt_slug’, 10, 3 );
[/code]
Now you managed to get until this step but you will hit a 404 if you try to view the link. Well that is because WordPress doesn’t recognize that post as a custom post type anymore. We need to fix and trick WordPress to think that the requested post is in fact a custom post type. Here is the way to do it:
[code language=”php”]
/**
* Some hackery to have WordPress match postname to any of our public post types
* All of our public post types can have /post-name/ as the slug, so they better be unique across all posts
* Typically core only accounts for posts and pages where the slug is /post-name/
*/
function custom_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’, ‘photograph’, ‘page’ ) );
}
}
add_action( ‘pre_get_posts’, ‘custom_parse_request_tricksy’ );
[/code]
And that was our lesson in WordPress slug removing. Now you managed to have your own custom post type permalinks without the slug.

Sandeep Kumar

Sandeep kumar is a web developer and a professinal blogger. He works for latest technologies and write articles what he learn. In ICodeHub he share web languages tutorials and other programming tips and tricks. if you need some help or have some suggestion then mail him at sandeep92pal@gmail.com without any hesitation. He believes in learning and sharing.