• Hi,

    I recently created some functionality on my site to remove the slug from the URL of a custom post type.

    ie. instead of accessing the custom post type from https://www.website.com/custom_type/article_name
    you could access it from https://www.website.com/article_name like any other regular post or page

    I had to do this in a “hacky” way.
    Here is a link to the page where I go this “hack”: https://www.markwarddesign.com/2014/02/12/remove-custom-post-type-slug-permalink/

    My question is, is there a better, non-hacky way to do this? And do you believe this “hack” will work in the long term? It seems to have worked since 2014, so it seems to me like its likely to keep working, but would be great to get some feedback from seasoned wordpress developers.

    Here is the code if you want to avoid visiting the site:

    
    Remove Post Type Slug from CPT
    /**
     * Remove the slug from published post permalinks. Only affect our CPT though.
     */
    function vipx_remove_cpt_slug( $post_link, $post, $leavename ) {
    
        if ( ! in_array( $post->post_type, array( 'event' ) ) || 'publish' != $post->post_status )
            return $post_link;
    
        $post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );
    
        return $post_link;
    }
    add_filter( 'post_type_link', 'vipx_remove_cpt_slug', 10, 3 );
    
    function vipx_parse_request_tricksy( $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'] ) )
            $query->set( 'post_type', array( 'post', 'your_post_type', 'page' ) );
    }
    add_action( 'pre_get_posts', 'vipx_parse_request_tricksy' );
    
    /**
     * Remove the slug from published post permalinks. Only affect our CPT though.
     */
    function vipx_remove_cpt_slug( $post_link, $post, $leavename ) {
     
        if ( ! in_array( $post->post_type, array( 'event' ) ) || 'publish' != $post->post_status )
            return $post_link;
     
        $post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );
     
        return $post_link;
    }
    add_filter( 'post_type_link', 'vipx_remove_cpt_slug', 10, 3 );
     
    function vipx_parse_request_tricksy( $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'] ) )
            $query->set( 'post_type', array( 'post', 'your_post_type', 'page' ) );
    }
    add_action( 'pre_get_posts', 'vipx_parse_request_tricksy' );
    

    Thanks in advance!
    Gab

    • This topic was modified 4 years, 10 months ago by gabez123.
    • This topic was modified 4 years, 10 months ago by gabez123.
Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Removing slug in url from custom post type’ is closed to new replies.