Please wait, loading...


Remove Slug from Custom Post Type URLs - Emerald Tech

March 13, 20140

Many times, you would like to remove the custom post type slug from urls. You don’t want frontend users to see you custom post type slug and to know you are using one. I kept looking for solutions, modified rewrite rules and did lots of things finally found a very easier solution. This solution works for hierarchical posts and non-hierarchical both. It also removes parent slug from hierarchical posts.  I hope this would help you guys. Here is the code.


function custom_post_type_link($permalink, $post, $leavename) {
if (!gettype($post) == 'post') {
return $permalink;
switch ($post->post_type) {
case 'your_custompost_type_slug':
$permalink = get_home_url() . '/' . $post->post_name . '/';

return $permalink;

The above code snippet will be put into your functions.php and it will remove slugs from all post urls. So where ever in the template you are displaying the_permalink() . But clicking those links you will get a 404, page not found if you don’t set the right query vars which are set in code given below:


function custom_pre_get_posts($query) {
global $wpdb;

if(!$query->is_main_query()) {

$post_name = $query->get('name');

$post_type = $wpdb->get_var(
'SELECT post_type FROM ' . $wpdb->posts . ' WHERE post_name = %s LIMIT 1',

switch($post_type) {
case 'your_custompost_type_slug':
$query->set('your_custompost_type_slug', $post_name);
$query->set('post_type', $post_type);
$query->is_single = true;
$query->is_page = false;

return $query;

So adding the above two snippets to funtions.php will remove custom post type slugs from urls. But last thing is:

don’t forget to replace your_custompost_type_slug in above code with your custom post type slug.  Happy coding!

courtesy:<a href=””>Ryan Sechrest. </a>

Yumna Tatheer

Leave a Reply

Your email address will not be published. Required fields are marked *

Copyright @ Yamna Tatheer CEO Emerald Technologies Registration Number: 3520166158082