Remove CPT slug but keep taxonomy slug rewrite

I have created a custom php form, And Also custom post type. now I need to make new metadata for custom posttype same as the contact form fields an make custom post type listing in backend.

Comments


  • single

    I am trying to remove the slug of my custom post type in the URL of WordPress but keep the taxonomy slugs in place.

    The taxonomy is hierarchical so there needs to be the option of having both taxonomy slugs in the url.

    For example, I need to recreate the following

    Current

    /product/balustrades/{CPT item}

    Required

    /balustrades/{CPT item}

    /balustrades/glass/{CPT item}

    Removing the CPT slug is relatively straight forward, and I can get the URLs to display exactly as I need them. However, I am getting a 404 when trying to access the pages. I have read in numerous places that this is to do with redirects needing to be put in place but the snippets I have come across don't seem to solve my issue. Here is what I currently have

    Remove slug:

    function remove_cpt_slug( $post_link, $post, $leavename ) {
    
    if ( 'product' != $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', 'remove_cpt_slug', 10, 3 );
    

    Custom post types/taxonomy:

    function cptui_register_my_cpts() {
    
    /**
    * Post Type: Products.
    */
    
    $labels = [
    "name" => __( "Products", "custom-post-type-ui" ),
    "singular_name" => __( "Product", "custom-post-type-ui" ),
    ];
    
    $args = [
    "label" => __( "Products", "custom-post-type-ui" ),
    "labels" => $labels,
    "description" => "",
    "public" => true,
    "publicly_queryable" => true,
    "show_ui" => true,
    "show_in_rest" => true,
    "rest_base" => "",
    "rest_controller_class" => "WP_REST_Posts_Controller",
    "has_archive" => false,
    "show_in_menu" => true,
    "show_in_nav_menus" => true,
    "delete_with_user" => false,
    "exclude_from_search" => false,
    "capability_type" => "post",
    "map_meta_cap" => true,
    "hierarchical" => true,
    "rewrite" => [ "slug" => "product", "with_front" => false ],
    "query_var" => true,
    "menu_position" => 20,
    "menu_icon" => "dashicons-store",
    "supports" => [ "title", "editor", "thumbnail", "custom-fields", "revisions" ],
    "taxonomies" => [ "post_tag", "usage" ],
    "show_in_graphql" => false,
    ];
    
    
    
    register_post_type( "product", $args );
    }
    
    add_action( 'init', 'cptui_register_my_cpts' );
    
    
    
    
    function cptui_register_my_taxes() {
    
    /**
    * Taxonomy: Usages.
    */
    
    $labels = [
    "name" => __( "Usages", "custom-post-type-ui" ),
    "singular_name" => __( "Use", "custom-post-type-ui" ),
    ];
    
    
    $args = [
    "label" => __( "Usages", "custom-post-type-ui" ),
    "labels" => $labels,
    "public" => true,
    "publicly_queryable" => true,
    "hierarchical" => true,
    "show_ui" => true,
    "show_in_menu" => true,
    "show_in_nav_menus" => true,
    "query_var" => true,
    "rewrite" => [ 'slug' => 'usage', 'with_front' => true, 'hierarchical' => true, ],
    "show_admin_column" => false,
    "show_in_rest" => true,
    "show_tagcloud" => false,
    "rest_base" => "usage",
    "rest_controller_class" => "WP_REST_Terms_Controller",
    "show_in_quick_edit" => false,
    "show_in_graphql" => false,
    ];
    register_taxonomy( "usage", [ "product" ], $args );
    }
    add_action( 'init', 'cptui_register_my_taxes' );
    

    I also have a rewrite in my permalink settings as showing here

    enter image description here

    Any help is much appreciated!

Add Comment