1

CPT: product Taxonomy: prod_cat

Current URLs

  1. Child Term domain.com/product-category/pulp/
  2. Hierarchical Child Term domain.com/product-category/pulp-iqf/pulp/apple/
  3. Single CPT Post domain.com/product/apple-juice-concentrate/

Desired URLs

  1. Child Term domain.com/pulp/
  2. Hierarchical Child Term domain.com/pulp-iqf/pulp/apple/
  3. Single CPT Post domain.com/pulp-iqf/pulp/apple/apple-juice-concentrate/

I've gone through the depths of answers on WPSE and believe me I've tried various combinations but I'm unable to set it this way. The regex filled solutions around creating your own rules trips me and hence would really appreciate if someone can explain what can be done to either achieve the Desired URL structure or the way to achieve something close to the desired URLs.

//Register product post type
function products_post_type() {

        $labels = array(
            'name'                  => 'Products',
            'singular_name'         => 'Product',
            'menu_name'             => 'Products',
            'name_admin_bar'        => 'Post Type',
            'parent_item_colon'     => 'Parent Product:',
            'all_items'             => 'All Products',
            'add_new_item'          => 'Add New Product',
            'add_new'               => 'New Product',
            'new_item'              => 'New Item',
            'edit_item'             => 'Edit Product',
            'update_item'           => 'Update Product',
            'view_item'             => 'View Product',
            'search_items'          => 'Search products',
            'not_found'             => 'No products found',
            'not_found_in_trash'    => 'No products found in Trash',
            'items_list'            => 'Items list',
            'items_list_navigation' => 'Items list navigation',
            'filter_items_list'     => 'Filter items list',
        );
        $args = array(
            'label'                 => 'Product',
            'description'           => 'Products Post Type',
            'labels'                => $labels,
            'supports'              => array( 'title', 'editor', 'excerpt', 'thumbnail', 'custom-fields', ),
            'hierarchical'          => true,
            'public'                => true,
            'show_ui'               => true,
            'show_in_menu'          => true,
            'menu_position'         => 5,
            'show_in_admin_bar'     => true,
            'show_in_nav_menus'     => true,
            'can_export'            => true,
            'has_archive'           => true,        
            'exclude_from_search'   => false,
            'publicly_queryable'    => true,
            'capability_type'       => 'post',
            'rewrite'               => array(
                                        'slug' => 'product',
                                        'hierarchical' => true,
                                        'with_front' => false,
                                        )
        );
        register_post_type( 'product', $args );

    }
    add_action( 'init', 'products_post_type', 0 );

    // Register Product Category
    function prod_cat() {

        $labels = array(
            'name'                       => 'Product Categories',
            'singular_name'              => 'Product Category',
            'menu_name'                  => 'Product Category',
            'all_items'                  => 'All Items',
            'parent_item'                => 'Parent Item',
            'parent_item_colon'          => 'Parent Item:',
            'new_item_name'              => 'New Item Name',
            'add_new_item'               => 'Add New Item',
            'edit_item'                  => 'Edit Item',
            'update_item'                => 'Update Item',
            'view_item'                  => 'View Item',
            'separate_items_with_commas' => 'Separate items with commas',
            'add_or_remove_items'        => 'Add or remove items',
            'choose_from_most_used'      => 'Choose from the most used',
            'popular_items'              => 'Popular Items',
            'search_items'               => 'Search Items',
            'not_found'                  => 'Not Found',
            'items_list'                 => 'Items list',
            'items_list_navigation'      => 'Items list navigation',
        );
        $args = array(
            'labels'                        => $labels,
            'hierarchical'                  => true,
            'public'                        => true,
            'show_ui'                       => true,
            'show_admin_column'             => true,
            'show_in_nav_menus'             => true,
            'show_tagcloud'                 => true,
            'rewrite'                       => array(   'slug' => 'product-category',
                                                        'hierarchical' => true,
                                                        'with_front' => false, 
                                                    )  
        );
        register_taxonomy( 'prod_cat', array( 'product' ), $args );

    }
    add_action( 'init', 'prod_cat', 0 );
1
  • What you desire can't be done by changing the options passed to register_post_type or register_taxonomy, it will require a custom rewrite rule, which means regular expressions are unavoidable
    – Tom J Nowell
    Commented Aug 28, 2017 at 20:44

1 Answer 1

0

CPT does't have much options for building custom permalinks. What you need can't be done without custom coding and creating new rewrite rules, and handling permalink parsing.

I have plugin for working with post types and taxonomies, and it includes options for custom permalinks and more. It is not a free plugin, but in many aspects is unique, and you can check it out via my profile home page.

2
  • Thanks for the revert. Although, when you say custom coding and creating new rewrite rules, and handling permalink parsing -- can you shed some light on how to go about that?
    – vajrasar
    Commented Aug 30, 2017 at 6:09
  • Changing permalinks is two part job: set the permalinks structure, and if you use some unusual elements (custom field for instance), you need to modify the code that parses URL to determine what the request is. My plugin allows you to register new post types and taxonomies, set all the things both of these support in WordPress. But, it adds many additional features, including the panel for setting permalinks, and so much more. You can check out all the features here: plugins.dev4press.com/gd-content-tools/features and if you want I can set up the full admin demo to try it out. Commented Aug 30, 2017 at 17:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.