Site icon Hip-Hop Website Design and Development

Permalinks: custom post type -> custom taxonomy -> custom sub taxonomy -> post

I am building a custom post type and its respective custom taxonomy.

Here is the code:

add_action( 'init', 'register_my_types' );

function register_my_types() {

    register_post_type( 'help',
        array(
            'labels' => array(
                'name' => __( 'Help Manager' ),
                'singular_name' => __( 'Help' )
            ),
            'public' => true,
            'has_archive' => true,
            'rewrite' => array(
                'slug' => 'help/%help_categories%',
                'with_front' => true
              )
        )
    );

    register_taxonomy( 'help_categories', array( 'help' ), array( 
            'hierarchical' => true, 
            'label' => 'Help Categories',
            'rewrite' => array(
                'slug' => 'help',
                'with_front' => true
              )
        )
    );
}

I am the doing a rewrite to include this in one pretty URL:

function so23698827_add_rewrite_rules( $rules ) {
    $new = array();
    $new['help/([^/]+)/(.+)/?$'] = 'index.php?help=$matches[2]';
    $new['help/(.+)/?$'] = 'index.php?help_categories=$matches[1]';

    return array_merge( $new, $rules ); // Ensure our rules come first
  }
  add_filter( 'rewrite_rules_array', 'so23698827_add_rewrite_rules' );

  /**
   * Handle the '%project_category%' URL placeholder
   *
   * @param str $link The link to the post
   * @param WP_Post object $post The post object
   * @return str
   */
  function so23698827_filter_post_type_link( $link, $post ) {
    if ( $post->post_type == 'help' ) {
      if ( $cats = get_the_terms( $post->ID, 'help_categories' ) ) {
        $link = str_replace( '%help_categories%', current( $cats )->slug, $link );
      }
    }
    return $link;
  }
  add_filter( 'post_type_link', 'so23698827_filter_post_type_link', 10, 2 );

I want to include any sub category in the URL. So for example, I have the following structure:

The URL structure should be the following www.domain.com/custom_post_type/main-category/sub-category/sample-post

Can you help me achieve the above please