Search code examples
url-rewritingpermalinksslug

Modify Custom Taxonomy slug with CPT slug


Hi please check my requirement bellow.

First of all, I have registered a CPT "directory" with a slug name "directory". Also, I have registered a custom taxonomy "business_category". I want to below permalink structure.

  1. CPT Archive link: www.domain.com/directory/
  2. Taxonomy link: www.domain.com/directory/category/TAXONOMY_NAME/
  3. CPT Single Page: www.domain.com/directory/POST_NAME

So, I have used the Below code.

public function __construct( $plugin_name, $version ) {

    $this->plugin_name = $plugin_name;
    $this->version = $version;
    $this->PT = 'cc-directory';
    $this->name = 'Directory';
    $this->singular_name = 'Directory';
    $this->slug = 'directory';

}

public function register_post_type() {

     // Get supported features for Directory post type
     $supports = apply_filters('cc_directory_supports', array('editor', 'title','thumbnail'));

     $labels = array(
        'name'                  => $this->name,
        'singular_name'         => $this->singular_name,
        'add_new'               => 'Add New',
        'add_new_item'          => 'Add New '   . $this->singular_name,
        'edit_item'             => 'Edit '      . $this->singular_name,
        'new_item'              => 'New '       . $this->singular_name,
        'all_items'             => 'All '       . $this->name,
        'view_item'             => 'View '      . $this->name,
        'search_items'          => 'Search '    . $this->name,
        'not_found'             => 'No '        . strtolower($this->name) . ' found',
        'not_found_in_trash'    => 'No '        . strtolower($this->name) . ' found in Trash',
        'parent_item_colon'     => '',
        'menu_name'             => $this->name
    );
    
    $args = array(
        'labels'                => $labels,
        'public'                => true,
        'publicly_queryable'    => true,
        'show_ui'               => true,
        'show_in_menu'          => true,
        'query_var'             => true,
        'rewrite'               => array( 'slug' => $this->slug ),
        'capability_type'       => 'post',
        'has_archive'           => true,
        'hierarchical'          => true,
        'menu_position'         => 11,
        'menu_icon'             => 'dashicons-book',
        'supports'              => $supports,
        'yarpp_support'         => true
    );

    register_post_type( $this->PT, $args );

    $plural_label = 'Categories';
    $singular_label = 'Category';

    register_taxonomy(
        'business_category',
        $this->PT,
        array(
            'label'                 => $plural_label,
            'labels'                => array(
                'name'              => $plural_label,
                'singular_name'     => $singular_label,
                'all_items'         => sprintf(__('All %s', 'claritycloud-directory'), $plural_label),
                'edit_item'         => sprintf(__('Edit %s', 'claritycloud-directory'), $singular_label),
                'view_item'         => sprintf(__('View %s', 'claritycloud-directory'), $singular_label),
                'update_item'       => sprintf(__('Update %s', 'claritycloud-directory'), $singular_label),
                'add_new_item'      => sprintf(__('Add New %s', 'claritycloud-directory'), $singular_label),
                'new_item_name'     => sprintf(__('New %s Name', 'claritycloud-directory'), $singular_label),
                'popular_items'     => sprintf(__('Popular %s', 'claritycloud-directory'), $plural_label),
                'search_items'      => sprintf(__('Search %s', 'claritycloud-directory'), $plural_label),
            ),
            'public'                => true,
            'show_ui'               => true,
            'show_in_rest'          => true,
            'hierarchical'          => true,
            'rewrite'               => array('slug' => 'business-category'),
        )
    );

}

Right now my URLs are :

  1. http://localhost/demo-project/directory/
  2. http://localhost/demo-project/business-category/antiques/
  3. http://localhost/demo-project/directory/a-lil-bit-of-sas/

It will be like below:

  1. http://localhost/demo-project/directory/
  2. http://localhost/demo-project/directory/category/antiques/
  3. http://localhost/demo-project/directory/a-lil-bit-of-sas/

Just need to modify the above 2nd URL.

Can anyone please advise me?

Thanks, Subhankar


Solution

  • Finally, I got the solution here:

    add_action('init', 'cpt_resources');

    function cpt_resources() {

    $labels = array(
        'name' => _x('Resources', 'snt'),
        'singular_name' => _x('Resource', 'snt'),
        'add_new' => _x('Add Resource', 'snt'),
        'add_new_item' => __('Add Resource'),
        'edit_item' => __('Edit Resource'),
        'new_item' => __('New Resource'),
        'view_item' => __('View Resource'),
        'search_items' => __('Search Resources'),
        'not_found' =>  __('Nothing found'),
        'not_found_in_trash' => __('Nothing found in Trash'),
        'parent_item_colon' => ''
    );
    
    $args = array(
        'labels' => $labels,
        'taxonomies' => array('resource_type'),
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'query_var' => true,
        'has_archive'           => true,
        'rewrite'   => array( 'slug' => 'resources' ),
        'capability_type' => 'post',
        'hierarchical' => false,
        'menu_position' => null,
        'supports' => array('title','thumbnail', 'editor' ),
      ); 
    
    register_post_type( 'resources_post_type' , $args );
    
     
    

    }

    function resource_type() {

    $labels = array(
        'name'                       => _x( 'Resource Types', 'Taxonomy General Name', 'snt' ),
        'singular_name'              => _x( 'Resource Type', 'Taxonomy Singular Name', 'snt' ),
        'menu_name'                  => __( 'Resource Types', 'snt' ),
        'all_items'                  => __( 'All Items', 'snt' ),
        'parent_item'                => __( 'Parent Item', 'snt' ),
        'parent_item_colon'          => __( 'Parent Item:', 'snt' ),
        'new_item_name'              => __( 'New Item Name', 'snt' ),
        'add_new_item'               => __( 'Add New Item', 'snt' ),
        'edit_item'                  => __( 'Edit Item', 'snt' ),
        'update_item'                => __( 'Update Item', 'snt' ),
        'view_item'                  => __( 'View Item', 'snt' ),
        'separate_items_with_commas' => __( 'Separate items with commas', 'snt' ),
        'add_or_remove_items'        => __( 'Add or remove items', 'snt' ),
        'choose_from_most_used'      => __( 'Choose from the most used', 'snt' ),
        'popular_items'              => __( 'Popular Items', 'snt' ),
        'search_items'               => __( 'Search Items', 'snt' ),
        'not_found'                  => __( 'Not Found', 'snt' ),
        'no_terms'                   => __( 'No items', 'snt' ),
        'items_list'                 => __( 'Items list', 'snt' ),
        'items_list_navigation'      => __( 'Items list navigation', 'snt' ),
    );
    $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' => 'resources/category')
    );
    register_taxonomy( 'resource_type', array( 'resources_post_type' ), $args );
    

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

    function resources_cpt_generating_rule($wp_rewrite) {

    $rules = array();
    $terms = get_terms( array(
        'taxonomy' => 'resource_type',
        'hide_empty' => false,
    ) );
    
    $post_type = 'resources_post_type';
    
    foreach ($terms as $term) {    
                
        $rules['resources/category/' . $term->slug . '/([^/]*)$'] = 'index.php?post_type=' . $post_type. '&resources_post_type=$matches[1]&name=$matches[1]';
                        
    }
    
    // merge with global rules
    $wp_rewrite->rules = $rules + $wp_rewrite->rules;
    

    }

    add_filter('generate_rewrite_rules', 'resources_cpt_generating_rule');

    Result : http://localhost/demo-wp/resources/

    http://localhost/demo-wp/resources/category/test-resources/

    http://localhost/demo-wp/resources/final-check/