Remove Custom Post Type Name from Permalink in WordPress

Nov 26th 22 byAdmin inWordPress
If you are a WordPress developer and love to create custom post types to addon the new feature in your project then this article is very helpful for you. When you create a new custom post type in WordPress then you can see that by default, its slug will show in the permalink URL structure. Adding slug to permalink displays the post type of the current post and creates the URLs longer which is hard to remember. Also, it creates an issue to rank articles in Google. So now I will cover all steps to remove slug from custom post type to make URL shorter and easy to remember.

Custom Post Type in WordPress

Recently, I created a custom post type called “Post Type” for a company. This resulted in permalinks such as example.com/post_type1/post-title As you can see, having permalinks like example.com/post_type1/post-title is not nearly as clean, also not easy to remember and search engine optimized. But having permalinks like example.com/post-title is nearly as clean, easy to remember, and SEO friendly. As SEO (Search Engine Optimization) experts always suggest making URL shorter highly rank in Google. I hope now you are clear with the article’s focused point. Now let’s see the solution:

Remove slug from custom post type

Step-1: Create your custom post type if you are not already created it till now. Step-2: Copy and Paste the following code to your theme functions.php file to filter the permalink for our custom post type so that all published posts don’t have the slug in their URLs:
<?php 
/**
 * Remove the slug from published post permalinks for our custom post types.
 */
add_filter( 'post_type_link', function( $post_link, $post, $leavename ) {

	$post_types = array(
		'post_type_1',
		'post_type_2'
	);

	if ( in_array( $post->post_type, $post_types ) && 'publish' === $post->post_status ) {
		$post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );
	}

	return $post_link;

}, 10, 3 );

/**
 * 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/
 */
add_action( 'pre_get_posts', function( $query ) {


	if ( ! $query->is_main_query() || 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'] ) ) {

			$post_types = array(
			'post', // important to  not break your standard posts
			'page', // important to  not break your standard pages
			'post_type_1',
			'post_type_12'
		);

		$query->set( 'post_type', $post_types );

	}

} );
?>
That’s it! Just replace  “online_services” with the slug of your custom post type in both code samples. After this Go to Settings > Permalinks and saving the permalink structure to end in /%postname%/ may also be necessary.

Wrapping Words!

Thanks for reading 🙏, I hope you found the Remove slug from custom post type in WordPress Without Plugin tutorial helpful for your project. Keep learning! If you face any problems – I am here to solve your problems.