Hello Everyone!
I hope you are doing well.
This time I am going to share one more useful knowledge to all wordpress developers who want to create a new custom url with same slug of custom taxonomy and custom post type.
I am going here to create a new custom post type "my_articles" with their custom taxonomy "my_article_term". You can use your own custom post type name at place of "my_articles".
Please follow given below steps:
Step-1
Create a new custom post type
if(!function_exists('my_acticle_post_type_func')){
function my_acticle_post_type_func()
{
$labels = array(
'name' => __( 'Article', 'mrwebsolution' ),
'singular_name' => __( 'Article', 'mrwebsolution' ),
'add_new' => __( 'Add New', 'mrwebsolution' ),
'add_new_item' => __( 'Add New Article', 'mrwebsolution' ),
'edit_item' => __( 'Edit Article', 'mrwebsolution' ),
'new_item' => __( 'New Article', 'mrwebsolution' ),
'view_item' => __( 'View Article', 'mrwebsolution' ),
'search_items' => __( 'Search Article', 'mrwebsolution' ),
'not_found' => __( 'No Article Found', 'mrwebsolution' ),
'not_found_in_trash' => __( 'No article Found In Trash', 'mrwebsolution' ),
'parent_item_colon' => '',
'menu_name' => __( 'Article', 'mrwebsolution' )
);
$args = array(
'labels' => $labels,
'menu_icon' => 'dashicons-admin-post',
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'hierarchical' => true,
'menu_position' => null,
'rewrite' => array( 'slug' => 'articles/%my_article_term%', 'with_front' => false ),
'has_archive' => 'articles',
'supports' => array( 'title', 'editor','thumbnail', 'page-attributes', 'comments', 'author')
);
register_post_type('my_articles', $args);
}
}
add_action('init', 'my_acticle_post_type_func');
/*-------------------------------------------------
End Articles Post Type
------------------------------------------------- */
Step -2
Create a new custom taxonomy
// hook into the init action and call create_my_article_taxonomies when it fires
add_action( 'init', 'create_my_article_taxonomies', 0 );
if(!function_exists('create_my_article_taxonomies'))
{
function create_my_article_taxonomies() {
$labels = array(
'name' => __('Article Categories', 'mrwebsolution'),
'singular_name' => __('Article', 'mrwebsolution'),
'search_items' => __('Search Categories', 'mrwebsolution'),
'all_items' => __('All Categories', 'mrwebsolution'),
'parent_item' => __('Parent', 'mrwebsolution'),
'parent_item_colon' => __('Parent:', 'mrwebsolution'),
'edit_item' => __('Edit Category', 'mrwebsolution'),
'update_item' => __('Update Category', 'mrwebsolution'),
'add_new_item' => __('Add New Category', 'mrwebsolution'),
'new_item_name' => __('New Category', 'mrwebsolution'),
'menu_name' => __('Categories', 'mrwebsolution'),
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'articles', 'with_front' => false ),
);
register_taxonomy( 'my_article_term', 'my_articles', $args );
}
}
/*-------------------------------------------------
End Articles Taxonomy
------------------------------------------------- */
Step-3
Generate new permalink for your newly created post type
function create_custom_permalinks( $post_link, $post ){
if ( is_object( $post ) && $post->post_type == 'my_articles' ){
$terms = wp_get_object_terms( $post->ID, 'my_article_term' );
if( $terms ){
return str_replace( '%my_article_term%' , $terms[0]->slug , $post_link );
}
}
return $post_link;
}
add_filter( 'post_type_link', 'create_custom_permalinks', 1, 2 );
/*-------------------------------------------------
End custom permalink
------------------------------------------------- */
URL Preview
- yourwebsite.com/articles/ => archive page
- yourwebsite.com/articles/%category%/ => custom taxonomy page
- yourwebsite.com/articles/%category%/%postname%/ => custom post
I hope you will enjoy this.

