Support » Plugin: Custom Post Type UI » Removing Post Type Slug From Custom Post Type Permalink

  • 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://wordpress.org/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 2 years, 10 months ago by pyoil.
    • This topic was modified 2 years, 10 months ago by pyoil.
    • This topic was modified 2 years, 10 months ago by pyoil.
Viewing 4 replies - 1 through 4 (of 4 total)
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.