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. Regrettably, this is not achievable using the built-in settings.
There are a large number of discussions on StackOverflow where users are seeking for a solution to this particular issue. It is possible to further personalize them by adding more rewrite rules and complicated PHP code snippets, if you are familiar with the WordPress API. However, this will need more time and work. The majority of WordPress users, however, will find this to be an inconvenient solution to their problem.
Using Permalink Manager, you can get around this limitation. The plugin, allows you to quickly and easily add category to custom post type permalinks. The main advantage of using it is that no programming experience is required on your behalf.
How to add custom taxonomy in custom post type permalink?
The URL examples provided below have been purposefully simplified in order to give you a better understanding of how this feature works. Although the example below uses custom taxonomies and post types, the same approach may be used to add categories to post permalinks.
Original WordPress permalinks (custom post type items)
New permalinks item customized with Permalink Manager (custom post type items with tax slugs included)
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“).
Now, scroll down to the bottom of the page 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“).
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.
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.
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
New permalink format with top-parent term slug only
New permalink format with lowest-child term slug only
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.
Now, to show the difference in a clear way, let us see what happens when lowest-level term is chosen. This instance will be similar to the previous example. If 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.
If you do not want Permalink Manager to support “primary category” you can disable it in the plugin settings. If you disable it, the default post permalink will take the lowest available category (with parents, if any).
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)
|$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.