0
votes

Currently, I'm creating a CPT that has its own custom taxonomy. It can select parent and child. But when I creating a news post, the taxonomy that in hierarchical order only select the last child.

I have tried to make the hierarchical to false, change 'with_front' => false, adding another custompost taxonomy on like, 'rewrite' => array('slug' => 'what-we-do/%wwd-category%/%wwd-category%'), but it gave results like 'localhost/CPT/taxo1/taxo1/article', repeating the last child selecting in 2 places.

function codex_custom_init() {
  $args = array(
    'public' => true,
    'label'  => 'What we do',
    'has_archive' => 'what-we-do',
    'query_var' => true,
    'rewrite' => array('slug' => 'what-we-do/%wwd-category%'),
    'taxonomies' => array( 'wwd-category' ),
    'supports'           => array( 'title', 'editor', 'thumbnail', 'revisions' ),
  );
  register_post_type( 'what-we-do', $args );

  register_taxonomy(
      'wwd-category',
      'what-we-do',
      array(
        'label' => __( 'Categories WWD' ),
        'hierarchical' => true,
        'query_var' => true,
        'rewrite' => array( 'slug' => 'what-we-do', 'with-front' => false)
      )
  );
}

add_action( 'init', 'codex_custom_init' ,1);

function tm_wwd_category_post_link( $post_link, $id = 0 ){ 
  $post = get_post($id);  
      $terms = wp_get_object_terms( $post->ID, 'wwd-category' );
      if( $terms ){
          return str_replace( '%wwd-category%' , $terms[0]->slug , $post_link );
} else {
    return str_replace( '%wwd-category%/' , '' , $post_link );
}

  return $post_link;  
}
add_filter( 'post_type_link', 'tm_wwd_category_post_link', 1, 3 );

The current result that I have achieved is

*http:// localhost/CPTUI/custom-taxonomy/postname.

But the actual result that I'm trying to achieve is

*http:// localhost/CPTUI/parent/parent-child/child/postname/

Thank you for your time to answer this question.

Parent-Child level for customtaxonomy

The order before publishing the new post

The order on permalink only shows the last of the taxonomy

The hierarchical is pushing selected taxonomy out-of-order

1

1 Answers

0
votes

I already got the answer, use Wordpress plugin called WP Better Permalinks.