0
votes

i have a custom post type which is products and has the custom taxonomy called categories. Under this categories i have a parent category and sub category.Now when i publish any post inside the post type products the URL structure should be xyz.com/products/parentcategory/subcategory/postname. But the URL structure is coming xyz.com/products/parentcategory/postname or xyz.com/products/subcategory/postname

$labels = array(
    'name'               => _x('products', 'post type general name', 'your-plugin-textdomain' ),
    'singular_name'      => _x( 'products', 'post type singular name', 'your-plugin-textdomain' ),
    'menu_name'          => _x( 'products', 'admin menu', 'your-plugin-textdomain' ),
    'name_admin_bar'     => _x( 'products', 'add new on admin bar', 'your-plugin-textdomain' ),
    'add_new'            => _x( 'Add New products', 'book', 'your-plugin-textdomain' ),
    'add_new_item'       => __( 'Add New products', 'your-plugin-textdomain' ),
    'new_item'           => __( 'New products', 'your-plugin-textdomain' ),
    'edit_item'          => __( 'Edit products', 'your-plugin-textdomain' ),
    'view_item'          => __( 'View products', 'your-plugin-textdomain' ),
    'all_items'          => __( 'All products', 'your-plugin-textdomain' ),
    'search_items'       => __( 'Search products', 'your-plugin-textdomain' ),
    'parent_item_colon'  => __( 'Parent products:', 'your-plugin-textdomain' ),
    'not_found'          => __( 'No products found.', 'your-plugin-textdomain' ),
    'not_found_in_trash' => __( 'No products found in Trash.', 'your-plugin-textdomain' )
);

$args = array(
    'labels'             => $labels,
    'description'        => __( 'Description.', 'your-plugin-textdomain' ),
    'public'             => true,
    'publicly_queryable' => true,
    'show_ui'            => true,
    'show_in_menu'       => true,
    'query_var'          => true,
    'rewrite'            => array( 'slug' => 'products/%categories%' ),
    'capability_type'    => 'post',
    'has_archive'        => false,
    'hierarchical'       => false,
    'menu_position'      => null,
    'supports'           => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ),
    'order' => 'ASC'
);

register_post_type( 'products', $args );
///*******************Taxonomy register**************************/
add_action('init', 'create_products_taxonomy');
function create_products_taxonomy()
{
    $labels = array(
        'name' => _x( 'categories', 'taxonomy general name' ),
        'singular_name' => _x( 'categories', 'taxonomy singular name' ),
        'search_items' =>  __( 'Search categories' ),
        'all_items' => __( 'All categories' ),
        'edit_item' => __( 'Edit categories' ),
        'update_item' => __( 'Update categories' ),
        'add_new_item' => __( 'Add New categories' ),
        'new_item_name' => __( 'New categories Name' ),
        'menu_name' => __( 'categories' ),
    );
    register_taxonomy('categories',array('products'), array(
        'public' => true,
        'hierarchical' => true,
        'labels' => $labels,
        'show_ui' => true,
        'show_admin_column' => true,
        'query_var' => true,
        'show_in_rest' => true,
        'show_in_quick_edit' => true,
        'meta_box_cb' => false,
        'rewrite' => array( 'slug' => 'products' ),
    ));

}

function wpa_course_post_link( $post_link, $id = 0 ){
    $post = get_post($id);
    if ( is_object( $post ) ){
        $terms = wp_get_object_terms( $post->ID, 'categories' );
        if( $terms ){
            return str_replace( '%categories%' , $terms[0]->slug.'/xyz', $post_link );
        }
    }
    return $post_link;
}
add_filter( 'post_type_link', 'wpa_course_post_link', 1, 3 );
1

1 Answers

0
votes

You can do this from your WordPress permalink. Go to Settings>Permalinks in Product permalinks check Custom base and past this /products/%category%/%postname%/.