How To Add Category To Post (Custom Post Type) URLs?

The most well-known shortcoming of the WordPress content management system is probably the lack of flexibility in terms of editing permalinks. One example of this limitation is the inability to add category slugs to custom post type items' permalinks.

When it comes to native post URLs, adding a category should not be an issue. The simplest method to accomplish this is to simply use native permalink settings. However, adding a custom taxonomy slug to a custom post type's URL is not that straightforward.

If you know how to code, you may do this by adding more rewrite rules with PHP code snippets. This will require a lot of time and the typical WordPress user will find this solution inconvenient.

With Permalink Manager, you can do this even if you do not know how to code. The plugin allows you to quickly and simply add taxonomies to custom post type permalinks.

Add category to custom permalinks

How to add custom taxonomy in custom post type permalink?

Before we continue, have a look at the following sample to see how the plugin might help you in rearranging your permalinks. The below example has been simplified on purpose to clearly demonstrate how it works.

Original WordPress permalinks (custom post type items)

http://example.com/product/%product%

http://example.com/product/cotton-t-shirt/
http://example.com/car/ford-focus/

New permalinks item customized with Permalink Manager (custom post type items with tax slugs included)

http://example.com/%product_cat%/%product%

http://example.com/calvin-klein/tops/t-shirts/cotton-t-shirt/
http://example.com/diesel/ford/ford-focus/

The procedure is straightforward, and you should have no difficulty completing it. If you want to include custom taxonomy slugs in your WordPress permalinks, you'll need to go to the Permalink Manager section ("Tools -> Permalink Manager -> Permastructures").

Permastructures section where you can add category to custom post type permalinks
The initial permastructure settings.

Now, scroll down until you find the custom post type that you want to adjust. In this simple example, we are adding two taxonomies to the permalinks for the "Cars" post type ("Manufacturer" and "Fuel").

How to edit Cars permalinks?
You may change the settings for each post type independently.
The custom field permastructure tags should be structured as follows: %custom_taxonomy_name%.

After that, you should insert the tags into the permastructure. The names of custom taxonomies are also the permastructure tags. If necessary, you may locate all of the possible permastructure tags in the "Permastructure tags" section.

New permastructure settings for custom post type
For the sake of this example, we shall use the %manufacturer% and %fuel%. The tags will vary depending on your specific case.
New permalink formats and existing permalinks

The taxonomy slugs will be automatically incorporated in all new post URLs. If you want to use the new formats on existing URLs, you will need to regenerate them.

A sample post permalink with category slug included
Both Manufacturer & Fuel terms' slugs are now present in single Car permalink.

How to remove parent category slugs from post permalinks?

Each taxonomy registered with WordPress can be either hierarchical or nonhierarchical. The first one allows you have parent and child terms, same as with inbuilt categories. For example, you may have a custom taxonomy named "countries" and add parent term named "Europe" with child terms such as "Germany", "France", and "Italy". The second type (nonhierarchical) will function identically to post tags.

Using Permalink Manager, you may add both hierarchical and non-hierarchical taxonomies to post permalinks. In addition to this, you can configure the plugin to not include the all category slugs in post permalinks.

With SEO in mind, there are several cases when it may be preferable to keep the URLs of custom post type items as short as possible. The instructions below may be helpful if you want to add a single term slug to post permalinks but not the whole trail including their parent terms.

Depending on your requirements, you may either add a single slug of the highest-level parent or the lowest-level child.

  • To use just the top parent slug, append the suffix "_top" to the end of the taxonomy permastructure tag.
    For example, replace %product_cat% with %product_cat_top%.
  • In the latter case, the suffix added to the end of the tag should be "_flat" to use the slug of the lowest-level term.
    For example, replace %product_cat% with %product_cat_flat%.

Initial permalink format
http://example.com/%product_cat%/%product%

http://example.com/clothing/t-shirts/cotton/happy-ninja
http://example.com/clothing/jackets/leather/biker-black

New permalink format with top-parent term slug only
http://example.com/%product_cat_top%/%product%

http://example.com/clothing/happy-ninja
http://example.com/clothing/biker-black

New permalink format with lowest-child term slug only
http://example.com/%product_cat_flat%/%product%

http://example.com/cotton/happy-ninja
http://example.com/leather/biker-black

Remove parent slug from Product permalinks.
New product permalinks will include only one (lowest-level) product category slug.

Primary category support

Permalink Manager is compatible with the "primary category" functionality of Yoast SEO, The SEO Framework, RankMath, and SEOPress. In summary, by default, the plugin replaces the "%taxonomy%" (e.g., %department%) tag in post permalinks with the slugs of actual categories' that are selected as the primary category.

Let us break it down using the most straightforwards examples possible. As you can see in the example below, the top-level category ("Sales") was chosen as the "primary category", and as a result, only its slug will be used in the URL for the post.

Top category selected
In this case, the top-category was chosen as the "primary category".

To make the difference clear, let us look at what happens when the lowest-level term is chosen. When the child category is selected, the slugs of all of its parent categories are included in the post URL, as shown in the screenshot below.

Lowest level term selected
If "Switzerland" is selected, Permalink Manager will follow the category hierarchy and append the slugs of parent categories ("sales/europe/switzerland") to the post permalink.

You may deactivate Permalink Manager's support for "primary category" in the plugin options. After you disable it, the default post permalink will use the lowest available category (including its parents, if available).

You can disable "Primary category" support in Permalink Manager settings.

How to modify term slug in post permalinks programmatically?

If you know how to code, you can modify the category slug that is used when the post's default URL is generated. In particular, you may create a custom function that will replace the %taxonomy% tag (eg. %category%), which is dynamically populated with the slug of the category chosen for the post.

permalink_manager_filter_term_slug($slug, $selected_term, $post, $all_terms, $taxonomy)
Variable Description
$slug The slug or slugs trail used to replace %taxonomy% tag (eg. %category%) in default post permalinks (string)
Example input: europe/central-europe/slovakia
$selected_term Term object chosen to be used in the default permalink of a post (WP_Term)
$post The post object for which the default permalink is generated (WP_Post)
$all_terms Array of all terms that are linked to the post that the default URL is generated for (WP_Term[])
$taxonomy The name of the taxonomy (string)
Example input: category

How to add multiple top-parent categories' slugs to single post permalink?

Below is a simple example code snippet that demonstrates how you can use it to have Permalink Manager add the slugs of two top-parent categories to a single post permalink using the function. It may also be used for other post types and taxonomies if you modify it to your needs.

/**
* The snippet changes the category slug used when default custom permalink is generated for a post
*/
function pm_multiple_top_categories_in_uri($slug, $selected_term, $post, $all_terms, $taxonomy) {
	// Use the snippet only for ‘category’ slug in ‘post’ custom permalinks
	if(empty($post->post_type) || $post->post_type !== 'post' || $taxonomy !== 'category') {
		return $slug;
	}
	if(!empty($all_terms) && count($all_terms) > 1) {
		// Get only top-level categories
		$top_parent_terms = wp_filter_object_list($all_terms, array('parent' => 0));
		if(!empty($top_parent_terms) && count($top_parent_terms) > 1) {
			// Reset array keys
			$top_parent_terms = array_values($top_parent_terms);
			// We need two slugs, therefore the loop will repeat until $slugs_counter is “2”
			$top_parent_terms = array_slice($top_parent_terms, 0, 2);
			foreach($top_parent_terms as $top_parent_term) {
				if(empty($top_parent_term->slug)) { continue; }
				$new_category_slug = (empty($new_category_slug)) ? $top_parent_term->slug : "{$new_category_slug}/{$top_parent_term->slug}";
			}
		}
	}
	return (!empty($new_category_slug)) ? $new_category_slug : $slug;
}
add_filter('permalink_manager_filter_term_slug', 'pm_multiple_top_categories_in_uri', 10, 5);
Go up