Skip to content

Instantly share code, notes, and snippets.

@wpexplorer
Last active October 7, 2022 01:27
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save wpexplorer/9745717 to your computer and use it in GitHub Desktop.
Save wpexplorer/9745717 to your computer and use it in GitHub Desktop.
Remove Custom Post Type Slug
<?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 ) {
// 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'] ) ) {
$post_types = array(
'post', // important to not break your standard posts
'page', // important to not break your standard pages
'post_type_1',
'post_type_2'
);
$query->set( 'post_type', $post_types );
}
} );
@Soullighter
Copy link

Thank you!

@timwd01
Copy link

timwd01 commented Aug 29, 2019

Thank you for this, do you have any idea how to 404 the old URLs with slugs? Because this creates duplicate content issues.

@Soullighter
Copy link

@timwd01 did you go to permalinks in admin and resave them?

@timwd01
Copy link

timwd01 commented Aug 30, 2019

@Soullighter I did, it still seems to be accessible on the old slugs, as well as without a slug.

@yfchild
Copy link

yfchild commented Dec 2, 2019

Hi. It´s perfect but doesn' t parse with languages.

example: www.domain.com/slugremove/urlxxxx with main language (in my case FR) www.domain.com/urlxxxx = perfect
example: www.domain.com/es/slugremove/urlxxxx with main language (spanish) www.domain.com/es/urlxxxx = redirect to home
example: www.domain.com/en/slugremove/urlxxxx with main language (english) www.domain.com/en/urlxxxx = redirect to home also

i try since several days, but not success, i try rewrite also with rank math seo= nothing.

I think, it´s because 'slugremove' it´s the same word in 3 language. Can you suggest me something or idea update code to rewrite.

Can you help me?

@wpexplorer
Copy link
Author

@yfchild - This would be an issue with the way in which the language plugin is doing redirects and not the code itself. So it will likely depend on the translation plugin you are using, you should ask the translation plugin developer.

@vijaysmith
Copy link

This doesn't work for Child posts, its resulting into 404!

@wpexplorer
Copy link
Author

@vijaysmith - This could be an issue perhaps with the "with_front" parameter. By default "child posts" are for standard pages not custom post types so it could just be an issue with WordPress. What's the purpose of Child Posts? why not just create a taxonomy for your post type?

@TopDMA
Copy link

TopDMA commented Jan 19, 2021

Hi i have 3 custom post type, and this code work only for one from them. How i can do this for all? Thank you

@wpexplorer
Copy link
Author

wpexplorer commented Jan 19, 2021

@TopDMA - just add your types in an array and alter the code a bit. I updated the original gist since I'm sure most would like to affect multiple types.

@wpexplorer
Copy link
Author

If you need a plugin for adding post types to your site check out our Post Types Unlimited Plugin: https://wordpress.org/plugins/post-types-unlimited/

@TopDMA
Copy link

TopDMA commented Jan 20, 2021

Thank You!!

@vlashkevych
Copy link

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment