• Resolved pyoil

    (@pyoil)


    I been looking far and wide to remove the post-type slug from the custom post type permalink and I have finally found the way to do it right.

    What I am trying to do is change website.com/posttype/post-name to look like this website.com/post-name

    I am posting the solution hoping it might help others.

    Step 1 – In WordPress Custom Post Type UI Plugin > Add/Edit Post Type > Edit Post Type > Custom Rewrite Slug > Set Value As “/”

    Step 2 – You need to add PHP Code to your website. Easiest way to do it is by installing Code Snippets Plugin https://www.ads-software.com/plugins/code-snippets/

    Step 3 – Add the slug of your custom post type to ‘race’ separated by comma in the below code snippet. I have added my slug, productpage. You can just replace ‘race’, ‘productpage’ with your desired post type slugs separated by a comma.

    
    /**
    Remove the slug from published post permalinks. Only affect our custom post type, though.
    
    Add the slug of your custom post type to 'race' separated by a comma in the below code snippet.
    **/
    
    function gp_parse_request_trick( $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','page','race', 'productpage' ) );
        }
    }
    add_action( 'pre_get_posts', 'gp_parse_request_trick' );
    

    Step 4 – Click Save Changes & Activate. Note If you just save changes, then the changes will not take effect. You must click Save Changes & Activate in the Code Snippets Menu.

    Hope this helps someone.

    • This topic was modified 7 years, 7 months ago by pyoil.
    • This topic was modified 7 years, 7 months ago by pyoil.
    • This topic was modified 7 years, 7 months ago by pyoil.
Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Only thing I have to contribute is this long-ish thread about the topic as well https://wordpress.stackexchange.com/questions/203951/remove-slug-from-custom-post-type-post-urls

    Not a fan of setting slug to “/” because that’s not a slug. It’s meant to be a separator for the query args for the request. However, I also can’t control it actually working, and in my mind, tricking WP core.

    Thread Starter pyoil

    (@pyoil)

    If there is a way to put this in as a feature in the plugin, that would be great.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    I think its best chance would be in CPTUI-Extended, but not the free plugin. While not the first time it’s been asked about, it’s definitely something not to take lightly and can be a bit specific to each user’s site. As you noted above, you did quite a lot of googling to find a possible solution that seemed to work.

    @pyoil
    Did the above code work for you.? I am searching a lot to remove base slug of CPT

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Removing Post Type Slug From Custom Post Type Permalink’ is closed to new replies.