[Resolved] Removing CPT slug from post url

Home Forums Support [Resolved] Removing CPT slug from post url

Home Forums Support Removing CPT slug from post url

  • This topic has 5 replies, 4 voices, and was last updated 3 years ago by David.
Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
  • #472878

    Hello GP Team! As I said on a previous thread, I was using Pods to create a manage my CPT and taxonomies. I’ve desisted from that because of one reason: It doesn’t allow you to remove the CPT slug from the posts’ url. So as I’m already using the official Child Theme, I’m currently trying GenerateWP, which brings you the needed code for registering a CPT.

    This is the actual code for my CPT:

    if ( ! function_exists('critica_post_type') ) {
    // Register Custom Post Type
    function critica_post_type() {
    	$labels = array(
    		'name'                  => 'Críticas',
    		'singular_name'         => 'Crítica',
    		'menu_name'             => 'Críticas',
    		'name_admin_bar'        => 'Crítica',
    		'archives'              => 'Archivos',
    		'attributes'            => 'Atributos',
    		'parent_item_colon'     => 'Parent Item:',
    		'all_items'             => 'Todas las críticas',
    		'add_new_item'          => 'Agregar Crítica',
    		'add_new'               => 'Agregar nueva',
    		'new_item'              => 'Nueva crítica',
    		'edit_item'             => 'Editar crítica',
    		'update_item'           => 'Actualizar crítica',
    		'view_item'             => 'Ver crítica',
    		'view_items'            => 'Ver críticas',
    		'search_items'          => 'Buscar crítica',
    		'not_found'             => 'No se encontró',
    		'not_found_in_trash'    => 'Not found in Trash',
    		'featured_image'        => 'Imagen destacada',
    		'set_featured_image'    => 'Fijar portada',
    		'remove_featured_image' => 'Remover portada',
    		'use_featured_image'    => 'Usar como portada',
    		'insert_into_item'      => 'Insertar a crítica',
    		'uploaded_to_this_item' => 'Uploaded to this item',
    		'items_list'            => 'Items list',
    		'items_list_navigation' => 'Items list navigation',
    		'filter_items_list'     => 'Filter items list',
    	$rewrite = array(
    		'slug'                  => 'critica',
    		'with_front'            => false,
    		'pages'                 => false,
    		'feeds'                 => false,
    	$args = array(
    		'label'                 => 'Crítica',
    		'description'           => 'Análisis profundos de series y películas',
    		'labels'                => $labels,
    		'supports'              => array( 'title', 'editor', 'thumbnail' ),
    		'taxonomies'            => array( 'category', 'post_tag' ),
    		'hierarchical'          => false,
    		'public'                => true,
    		'show_ui'               => true,
    		'show_in_menu'          => true,
    		'menu_position'         => 5,
    		'menu_icon'             => 'dashicons-media-text',
    		'show_in_admin_bar'     => true,
    		'show_in_nav_menus'     => true,
    		'can_export'            => true,
    		'has_archive'           => true,
    		'exclude_from_search'   => false,
    		'publicly_queryable'    => true,
    		'rewrite'               => $rewrite,
    		'capability_type'       => 'page',
    	register_post_type( 'critica_post_type', $args );
    add_action( 'init', 'critica_post_type', 0 );

    And I’ve found an article sharing a code that would “definitely” erase the slug from the actual post permalink. The thing is, I’ve tried the code but it gives me a error 500, I paste it below:

    /* Remove the slug from published post permalinks. Only affect our custom post type, though.*/
    function shapeofweb_remove_cpt_slug( $post_link, $post, $leavename ) { 
        if ( 'your_post_type' != $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', 'shapeofweb_remove_cpt_slug', 10, 3 );
    function shapeofweb_parse_request_trick( $query ) {
        // Only noop the main query
        if ( ! $query->is_main_query() )
        // Only noop our very specific rewrite rule match
        if ( 2 != count( $query->query ) || ! isset( $query->query['page'] ) ) {
        // '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', 'your_post_type' ) );
    add_action( 'pre_get_posts', 'shapeofweb_parse_request_trick' );

    I want to know if you know a way to achieve this. I’m interested on this basically for SEO purposes.

    GeneratePress 2.0.1
    GP Premium 1.5.6

    I’ve found a solution, it’s so simple thas is kind of a joke.

    To the rewrite you just have to change your current slug from ‘cpt-slug’ to ‘/’ and the with-front: ‘false’. And done, you just resave permalinks and the url shouldn’t display the cpt slug anymore.

    It’s a wonderful thing being able to extend WP capabilities without the need of a plugin.

    Lead Developer
    Lead Developer

    Glad you got it working – thanks for sharing the solution 🙂


    I have the exact same problem and tried your solution Roverd. It worked, but all posts and pages stop working, so I had to remove it again.

    Is there another way to remove the CPT slug while keeping posts and pages working?


    I finally got it working thanks to this plugin: https://wordpress.org/plugins/remove-taxonomy-base-slug/

    Customer Support

    Glad to hear you found the right solution and thanks for sharing.

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.