0

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:

  • Custom Post Type
    • Main Category
      • Sub Category
        • Sample Post

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

7
  • If you've already managed to get the custom post type and custom taxonomy up to speed, the problem/challenge seems to be with the permalinks. I suggest looking there. You can find these settings in the Wordpress Dashboard > Settings > Permalinks.
    – Bram
    Jan 30 '19 at 12:36
  • are you getting 404 error? Jan 30 '19 at 12:40
  • I wish permalinks was the problem. It's not. I am not able to get the custom post type and the custom taxonomy to work under the structure I explained above
    – Jojo
    Jan 30 '19 at 13:08
  • @ChinmoyKumarPaul - Its not about a 404 its about not managing to get the URL structure combined between the CPT and the taxonomy
    – Jojo
    Jan 30 '19 at 13:09
  • I am getting same kind of posts here wordpress.stackexchange.com/questions/53515/… wordpress.stackexchange.com/questions/94817/… Check them properly Jan 30 '19 at 13:14
0

you can try with this plugin (or see its code)

https://wordpress.org/plugins/wp-better-permalinks/

It allows you change the custom friendly permalinks structure to Custom Post Type > Taxonomy > Post and Custom Post Type > Taxonomy instead of default WordPress structure. it Provides different permalinks structure pattern: Custom Post Type > Single Term (or Term tree) > Post Custom Post Type > Post (when no term is selected) Custom Post Type > Single Term (or Term tree)

It helped me a lot with this issue...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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