How To Remove Slug From Custom Post Type In WordPress

How to remove slug from custom post type in WordPress
2527 Views
3.7
(6482)

When you create a custom post type in WordPress, by default it adds a slug to the URL of each post. This slug can help organize your content, but it can also be a hindrance if you want to remove it or change it to something else.

Fortunately, it’s not too difficult to remove the slug from your custom URL post type in WordPress. Here’s how to do it:

  1. Open your functions.php file in your theme’s folder.
  2. Add the following code to the file:

Step 1. Remove Slug from the Permalink

<?php
add_filter( 'post_type_link', 'cxc_product_remove_slug', 10, 3 );
function cxc_product_remove_slug( $post_link, $post, $leavename ) {

	// product is my post type
    if ( 'product' != $post->post_type || 'publish' != $post->post_status ) {
        return $post_link;
    }

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

    return $post_link;
}
?>

This code is used to remove the slug from a custom post type in WordPress. By default, when you create a custom post type, WordPress adds a slug to the URL of each post.

Step 2

<?php
add_action( 'pre_get_posts', 'cxc_parse_request_function' );
function cxc_parse_request_function( $query ) {

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

    if ( ! empty( $query->query['name'] ) ) {
        $query->set( 'post_type', 'product' ); // You can add multipe post type like array. ex: array( 'post', 'events', 'page'  )
    }
}
?>

This code is used to fix the 404 error that might occur after removing the slug from a custom post type in WordPress. WordPress might not be able to properly route the URL to the correct post or page. This can result in a 404 error.

The code you provided adds an action to the pre_get_posts hook in WordPress, which allows you to modify the query parameters before WordPress retrieves the posts.

The cxc_parse_request_function the function is called by the hook, and it checks to see if the query is the main query and if it has a page number in the query string.

If those conditions are met, the function sets the post type to “product”. This tells WordPress to look for posts of the “product” post type instead of trying to route the URL to a page or post with a missing slug.

FAQs

What is a slug in WordPress and why is it important?

A slug is part of the URL that comes after the domain name and identifies a specific post or page. In WordPress, it is generated based on the title of the post or page, but can be customized. It is important because it helps make URLs more descriptive and easier to remember.

Can I remove the slug from a custom post type without affecting its URL?

Yes, by using the code provided in this tutorial, you can remove the slug from a custom post type without affecting the URL of individual posts.

Will removing the slug from a custom post type affect its SEO?

It shouldn’t negatively affect the SEO of the custom post type or individual posts. However, it’s important to set up 301 redirects to the new URLs of the archive page and individual posts to prevent any negative impact on search engine rankings.

Can I add a different slug to a custom post type instead of removing it?

Yes, you can customize the slug of a custom post type by using the rewrite parameter when registering the post type. This can help make the URL more descriptive or easier to remember.

How useful was this blog?

Click on a star to rate it!

Average rating 3.7 / 5. Vote count: 6482

No votes so far! Be the first to rate this blog.

  • CodexCoach

    - Web Development Expert

    CodexCoach is a skilled tech educator known for easy-to-follow tutorials in coding, digital design, and software development. With practical tips and interactive examples, CodexCoach helps people learn new tech skills and advance their careers.

1 comment

Leave a comment

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