How To Remove Custom Post Slug From Url

Posted By : Renu Yadav | 30-Apr-2018

Wordpress is good for writing blogs. Wordpress handles easily all blogs. If you changed the URL of the blog then there is no problem of redirection. Like you shared the blog on any site or any social site, after that if you changed the URL of the blog. Then there is no issue, WordPress redirect the old URL to new URL of the blog. Wordpress has the functionality to redirect your old blogs URL to new URL of the blog. 

 

If you created the custom post type like Relevant Work. You write the posts under relevant work. When you go to the URL of the post. You see the post URL have relevant work slug. If you don't want to show relevant-work slug in your site URL. Like http://www.dummytext.com/relevant_work/internet-marketing-agency/.

 

Because of the above URL, you will get the issue on google ranking. So you have to remove relevant work slug from your URL.

function na_remove_slug( $post_link, $post, $leavename ) {

		if ( 'relevant_work' != $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', 'na_remove_slug', 10, 3 );


function na_parse_request( $query ) {

		if ( ! $query->is_main_query() || 2 != count( $query->query ) || ! isset( $query->query['page'] ) ) {
			return;
		}

		if ( ! empty( $query->query['name'] ) ) {
			$query->set( 'post_type', array( 'post', 'relevant_work', 'page' ) );
		}
}
add_action( 'pre_get_posts', 'na_parse_request' );

 

Above code will add to the function.php file. If you want to add above code in another file Like custom-posttype.php. You can add this code to custom-posttype.php file. Before adding the code to file, you need to create custom-posttype.php file under inc folder. After creating the file for the custom post. Add the above code in custom-posttype.php file. 

 

require get_parent_theme_file_path( '/inc/custom-posttype.php' );

 

Now you can add this file to your function.php file. After adding above line in your function.php file. Your relevant work slug will remove from your site URL. After the removal of relevant work, now you can see this URL. http://www.dummytext.com/internet-marketing-agency/. Remove relevant work slug from URL will help your page ranking and also your URL will look good.Url is very important for every website. So WordPress provide so many facilities to the developer, they can change the URL of blogs.

About Author

Author Image
Renu Yadav

Renu is an Associate Consultant -WordPress in Oodles. She likes watching movies and reading books.

Request for Proposal

Name is required

Comment is required

Sending message..