Skip Navigation

[Resolved] Custom Post Link

This thread is resolved. Here is a description of the problem and solution.

Problem:

How to remove the base slug of a custom post type?

Solution:

It is not recommended to do that as slugs need to be unique in WordPress.

You can do it by adding the custom code below:

function na_remove_slug( $post_link, $post, $leavename ) {
 
    if ( 'events' != $post->post_type || 'publish' != $post->post_status ) {
        return $post_link;
    }
 
    $post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );
 
    return $post_link;
}

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', 'events', 'page' ) );
    }
}
add_action( 'pre_get_posts', 'na_parse_request' );

Relevant Documentation:

https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

This topic contains 4 replies, has 2 voices.

Last updated by fahimS-2 1 year, 9 months ago.

Assisted by: Christopher Amirian.

Author
Posts
#2539735

Tell us what you are trying to do?
I am creating a custom post type. But when I am adding a custom post of that type the post slug is prepending on that post link. How I can remove it?

Is there any documentation that you are following?

Is there a similar example that we can see?

What is the link to your site?

#2539881

Christopher Amirian
Supporter

Languages: English (English )

Hi there,

That is how WordPress work. You can use a method like this to force it to go away:

https://wordpress.stackexchange.com/questions/203951/remove-slug-from-custom-post-type-post-urls

But please consider that it will cause an easy conflict in URLs.

For example if you have a page with the name of hello and a custom post item with the name of hello, WordPress will not know which one to show when you access:

YOURWEBSITE.com/hello

Thanks.

#2539909

There are so many codes on that page. Which one should I use?

#2540999

Christopher Amirian
Supporter

Languages: English (English )

Hi there,

This code:

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

    if ( 'events' != $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 );

And this code:

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', 'events', 'page' ) );
    }
}
add_action( 'pre_get_posts', 'na_parse_request' );

In both codes, you need to replace `events` with the slug of your custom post type.

Thanks.

#2542137

My issue is resolved now. Thank you!