How to create a permalink of custom post type with same slug as custom taxonomy?


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

/*-------------------------------------------------
 Sart Articles 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

/*-------------------------------------------------
 Start Article 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

/*-------------------------------------------------
 Start custom permalink
------------------------------------------------- */
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

  1. yourwebsite.com/articles/ => archive page
  2. yourwebsite.com/articles/%category%/ => custom taxonomy page
  3. yourwebsite.com/articles/%category%/%postname%/ => custom post

I hope you will enjoy this.

Advertisements

Sr. Software Engineer

Tagged with: , , , , ,
Posted in Wordpress

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Recent Posts
%d bloggers like this: