Taxonomy and custom post types are essential features of WordPress that allow users to categorize and organize their content effectively. Taxonomy allows you to group content together based on specific characteristics, while custom post types enable you to create custom content structures. By default, WordPress generates separate slugs for both taxonomy and custom post types, which can be problematic for users who want to keep their URLs consistent. Let’s explore how to keep taxonomy slug same as custom post type.
Table of Contents
In this article, we will discuss how you can keep taxonomy slug same as the custom post type slug and use the taxonomy slug as part of the custom post slug. We will provide a step-by-step guide with examples to make it easy for you to implement this functionality in your WordPress site.
Related: Rewrite Custom Post URL With Taxonomy or Category
Understanding Taxonomy and Custom Post Types
Before we dive into the process of keeping the taxonomy slug same as custom post type slug and using the taxonomy slug as part of the custom post slug, it’s important to understand the difference between taxonomy and custom post types.
Taxonomy in WordPress is a way to group content based on specific characteristics.
For example, you can create a taxonomy called “Categories” and use it to group your blog posts based on their topics. You can also create custom taxonomies to group content based on other characteristics, such as “Tags” or “Genres.”
Custom post types, on the other hand, allow you to create custom content structures. By default, WordPress comes with several post types, such as “Posts” and “Pages.” However, you can also create your own custom post types to organize your content in a way that makes sense for your website.
Assumptions
- You have a custom post type “ba_gallery” with url rewrite “before-after-photos”.
- You have a taxonomy linked to this custom post type with name “ba_gallery_category” with url rewrite same as or different then the custom post type. Here lets assume its url rewrite slug is same as “before-after-photos”.
Requirements
You want the custom posts to have the URL structure as below:
https://www.example.com/%ba_gallery%/%ba_gallery_category%/%your-post-slug%/
To rewrite URLs in the above format or structure. You can use the following code in your themes function.php file.
Rewrite Taxonomy URLs
function taxcustom_cpt_generating_rule($wp_rewrite) {
$rules = array();
$terms = get_terms( array(
'taxonomy' => 'ba_gallery_category',
'hide_empty' => false,
) );
$post_type = 'ba_gallery';
foreach ($terms as $term) {
$rules['before-after-photos/' . $term->slug . '/([^/]*)$'] = 'index.php?post_type=' . $post_type. '&ba_gallery=$matches[1]&name=$matches[1]';
}
// merge with global rules
$wp_rewrite->rules = $rules + $wp_rewrite->rules;
}
add_filter('generate_rewrite_rules', 'taxcustom_cpt_generating_rule');
Rewrite Custom Post URLs
function change_custompost_link( $permalink, $post ) {
if( $post->post_type == 'ba_gallery' ) {
$resource_terms = get_the_terms( $post, 'ba_gallery_category' );
$term_slug = '';
if( ! empty( $resource_terms ) ) {
foreach ( $resource_terms as $term ) {
$term_slug = $term->slug;
break;
}
}
$permalink = get_home_url() ."/before-after-photos/" . $term_slug . '/' . $post->post_name;
}
return $permalink;
}
add_filter('post_type_link',"change_custompost_link",10,2);
Final Outcome
Save the changes, flush the permalink. Settings >> Permalink by login to the WP backend.
The urls will get rewritten in the following format.
Taxonomy URL example:
https://www.example.com/before-after-photos/{{taxonomy-slug}}
Custom post URL example:
https://www.example.com/before-after-photos/{{taxonomy-slug}}/{{post-slug}}/
Also read: Implement Auto Suggestion in WordPress Search
How to Keep Taxonomy Slug Same as Custom Post Type Slug?
By default, WordPress generates separate slugs for taxonomy and custom post types, which can be problematic for users who want to keep their URLs consistent. However, you can easily keep the taxonomy slug same as the custom post type slug by using a plugin called “Custom Post Type Permalinks.”
Step 1: Install and Activate Custom Post Type Permalinks Plugin
The first step is to install and activate the “Custom Post Type Permalinks” plugin. You can do this by going to the “Plugins” page in your WordPress dashboard and searching for the plugin. Once you have found the plugin, click on the “Install Now” button and then activate the plugin.
Step 2: Update Your Custom Post Type Slug
The next step is to update your custom post type slug to match your desired taxonomy slug. For example, if you have a custom post type called “Books” and a taxonomy called “Genres,” you might want to update your custom post type slug to “genres-books.”
You can do this by adding the following code to your functions.php file:
function custom_post_type_permalink($post_link, $post) {
if ( 'books' === get_post_type($post) && 'publish' === $post->post_status ) {
$terms = wp_get_object_terms( $post->ID, 'genres' );
if( $terms ){
return str_replace( '%genres%' , $terms[0]->slug , $post_link );
}
}
return $post_link;
}
add_filter( 'post_type_link', 'custom_post_type_permalink', 1, 2 );
Step 3: Update Your Taxonomy Slug
The final step is to update your taxonomy slug to match your custom post type slug. You can do this by adding the following code to your functions.php file:
function custom_taxonomy_permalink($link, $term, $taxonomy) {
if ($taxonomy !== 'genres') {
return $link;
}
$post_type = 'books';
$post_type_permalink($post_link, $post) { if ( 'books' === $post_type ) { $terms = wp_get_object_terms( $post->ID, $taxonomy ); if( $terms ){ return str_replace( '%genres%' , $terms[0]->slug , $post_link ); } } return $post_link; } add_filter( 'term_link', 'custom_taxonomy_permalink', 10, 3 );
This code will update your taxonomy slug to match your custom post type slug. In this example, we have used the “Genres” taxonomy and the “Books” custom post type, but you can replace these with your desired taxonomy and custom post type.
Related: Implement Infinite Pagination in WordPress
Using Taxonomy Slug as Part of Custom Post Slug
In addition to keeping the taxonomy slug same as the custom post type slug, you may also want to use the taxonomy slug as part of the custom post slug. This can help to create more meaningful and SEO-friendly URLs.
To use the taxonomy slug as part of the custom post slug, you can use the same “Custom Post Type Permalinks” plugin and add a few lines of code to your functions.php file.
Step 1: Update Your Custom Post Type Slug
The first step is to update your custom post type slug to include the taxonomy slug.
For example, if you have a custom post type called “Books” and a taxonomy called “Genres,” you might want to update your custom post type slug to “genres/%genres%-books.“
You can do this by adding the following code to your functions.php file:
function custom_post_type_permalink($post_link, $post) { if ( 'books' === get_post_type($post) && 'publish' === $post->post_status ) { $terms = wp_get_object_terms( $post->ID, 'genres' ); if( $terms ){ return str_replace( '%genres%' , $terms[0]->slug , str_replace( 'genres/', 'genres/%genres%-' , $post_link ) ); } } return $post_link; } add_filter( 'post_type_link', 'custom_post_type_permalink', 1, 2 );
This code will update your custom post type slug to include the taxonomy slug as a prefix.
For example, if you have a book with the “Fiction” genre, the URL will look like this: “example.com/genres/fiction-books“.
Step 2: Update Your Permalink Structure
The final step is to update your permalink structure to include the new custom post type slug. You can do this by going to “Settings” > “Permalinks” in your WordPress dashboard and updating the permalink structure to include the custom post type slug.
Also read: Calculate Read Time of an Article or Textual Content
Benefits to Keep Taxonomy Slug Same as Custom Post Type
Keeping the taxonomy slug same as the custom post type slug can provide several benefits for your WordPress site.
Here are some of the main benefits:
1. Consistency in URL Structure
Having a consistent URL structure across your website can provide a better user experience for your visitors. When your URLs follow a predictable pattern, it can help users to navigate your site more easily and find the content they are looking for.
By keeping the taxonomy slug same as the custom post type slug, you can ensure that your URLs follow a consistent structure.
For example, if you have a custom post type called “Events” and a taxonomy called “Locations,” you might want the URL for an event in New York to look like this: “example.com/events/new-york-event.” By keeping the taxonomy slug the same as the custom post type slug, you can ensure that all of your event URLs follow this same structure.
2. Improved SEO
Having a clear and consistent URL structure can also help to improve your site’s SEO. Search engines use the URL as a ranking factor, so having a URL that includes relevant keywords can help your content to rank higher in search results.
By keeping the taxonomy slug same as the custom post type slug, you can ensure that your URLs include relevant keywords and are optimized for search engines.
For example, if you have a custom post type called “Recipes” and a taxonomy called “Ingredients,” you might want the URL for a recipe that includes chicken to look like this: “example.com/recipes/chicken-recipe.” This URL includes the keyword “chicken,” which can help the content to rank higher in search results for that keyword.
3. Easier to Manage
When your taxonomy slug is the same as the custom post type slug, it can also be easier to manage your website. This is because you only need to manage one slug for each post type, rather than separate slugs for each taxonomy.
For example, if you have a custom post type called “Products” and several taxonomies for product categories, it can be easier to manage your website if the URLs for all of your product pages follow the same structure. By keeping the taxonomy slug the same as the custom post type slug, you can ensure that all of your product URLs follow a consistent structure and are easier to manage.
Related: Implement Next and Previous Article Navigation Link With Title
Conclusion
By default, WordPress generates separate slugs for taxonomy and custom post types, which can be problematic for users who want to keep their URLs consistent. However, by using the “Custom Post Type Permalinks” plugin and a few lines of code, you can easily keep the taxonomy slug same as the custom post type slug and use the taxonomy slug as part of the custom post slug.
Also read: How to Secure Your WordPress Website from Hackers?
This can help to create more meaningful and SEO-friendly URLs, which can improve the user experience and help your website rank higher in search engine results. We hope that this article has provided a helpful guide for implementing this functionality in your WordPress site.