1. Home
  2. /
  3. Web Design & Development
  4. /
  5. WordPress
  6. /
  7. How to Rewrite Custom Post URL With Taxonomy or Category Slug Appended Before the Post Slug?

How to Rewrite Custom Post URL With Taxonomy or Category Slug Appended Before the Post Slug?

Rewrite Custom Post URL with Taxonomy or Category Slug

Custom post types and taxonomies are crucial components of WordPress, allowing you to create and organize your content in unique ways. However, by default, WordPress does not include taxonomy or category information in custom post URLs. This can be problematic when you’re trying to create a logical and intuitive URL structure for your site visitors. Fortunately, there’s a way to rewrite custom post URL with taxonomy or category information.

In this article, we’ll walk you through the steps of doing just that.

Also read: How to Secure Your WordPress Website from Hackers?

What Are Custom Post Types and Taxonomies?

Custom post types and taxonomies are WordPress features that allow you to create and organize content in a way that’s different from the standard WordPress posts and pages. Custom post types are used to create different types of content, such as products, events, or portfolio items.

Taxonomies, on the other hand, are used to group and categorize content within custom post types. Taxonomies can be hierarchical, such as categories, or non-hierarchical, such as tags.

Assumptions

  1. You have a custom post type “my_custom_post_type” with url rewrite “my-custom-post”.
  2. You have a taxonomy linked to this custom post type with name “my_custom_post_type_category” with url rewrite same as or different then the custom post type. Here lets assume its url rewrite slug is same as “my-custom-post”.

Requirements

You want the custom posts to have the URL structure as below:

https://www.example.com/%my_custom_post_type%/%my_custom_post_type_category%/%your-post-slug%/

Code to rewrite custom post URL with taxonomy or category slug

To rewrite URLs in the above format or structure. You can use the following code in your themes function.php file.

function my_custom_post_type_permalink_structure($post_link, $post, $leavename, $sample) {
    if ($post->post_type == 'my_custom_post_type') {
      $terms = wp_get_object_terms($post->ID, 'my_custom_post_type_category');
      if (!empty($terms)) {
        $term_slug = $terms[0]->slug;
      } else {
        $term_slug = 'uncategorized';
      }
      $post_link = home_url('/my-custom-post/' . $term_slug . '/' . $post->post_name . '/');
    }
    return $post_link;
  }
  add_filter('post_type_link', 'my_custom_post_type_permalink_structure', 10, 4);

Final Outcome

Once you’re done adding the above piece of code. Save the changes, flush the permalink from the WP backend settings.

The urls will get rewritten in the following format.

https://www.example.com/my-custom-post/{{taxonomy-slug}}/{{post-slug}}/

Related: Use Taxonomy Slug as Part of Custom Post Slug

Why Rewrite Custom Post URLs?

By default, WordPress does not include taxonomy or category information in custom post URLs. This can be problematic when you’re trying to create a logical and intuitive URL structure for your site visitors. For example, let’s say you have a custom post type for recipes, and you want to organize your recipes by course (appetizer, main course, dessert).

Without rewriting the URLs, the URLs for your recipes might look something like this:

example.com/recipe/chicken-alfredo

example.com/recipe/chocolate-cake

example.com/recipe/taco-salad

While these URLs might be technically correct, they’re not very user-friendly. It’s not immediately clear from the URL what course the recipe is for.

By including the course information in the URL, you can create URLs that are both logical and intuitive, like this:

example.com/recipe/appetizer/chicken-alfredo

example.com/recipe/dessert/chocolate-cake

example.com/recipe/main-course/taco-salad

Also read: Use Custom Social Icons in the HubSpot Email Template

How to Rewrite Custom Post URL With Taxonomy or Category Slug?

Now that we understand why we might want to rewrite custom post URL with taxonomy or category information, let’s walk through the steps of doing just that.

1. Install a plugin

The first step in rewriting your custom post URLs is to install a plugin. There are many plugins available that can help you with this, but we recommend using the Custom Post Type Permalinks plugin. This plugin allows you to easily customize your custom post URLs without having to write any code.

To install the plugin, navigate to Plugins > Add New in your WordPress dashboard, and search for “Custom Post Type Permalinks.” Click the “Install Now” button, and then activate the plugin.

2. Configure the plugin settings

Once you’ve installed and activated the plugin, you’ll need to configure the settings to rewrite your custom post URLs with taxonomy or category information. To do this, navigate to Settings > Permalinks in your WordPress dashboard, and then scroll down to the “Custom Post Type Permalinks” section.

In this section, you’ll see a list of your custom post types. Click the “Edit” link next to the custom post type that you want to rewrite the URLs for.

3. Rewrite the custom post URLs

After you’ve clicked the “Edit” link, you’ll see a screen that allows you to configure the URL structure for your custom post type. In the “Permalink Structure” field, you’ll see a default structure for your custom post URLs. To rewrite the URLs with taxonomy or category information, you’ll need to add the appropriate tags to the structure.

To include the taxonomy slug in the URL, you’ll need to use the %category% tag. For example, if you wanted to include the recipe course as a taxonomy in our example from earlier, we would add the %category% tag to the permalink structure like this:

recipe/%category%/%postname%/

This structure will include the category (course) slug in the URL before the post name. So, the URL for our chicken alfredo recipe would be:

example.com/recipe/main-course/chicken-alfredo

You can also include multiple taxonomies in your custom post URLs by adding additional tags to the permalink structure. For example, if we wanted to include both course and cuisine as taxonomies, we could use this structure:

recipe/%category%/%cuisine%/%postname%/

This structure would include both the course and cuisine slugs in the URL before the post name. So, the URL for our chicken alfredo recipe with an Italian cuisine tag would be:

example.com/recipe/main-course/italian/chicken-alfredo

4. Save the changes

Once you’ve configured the permalink structure for your custom post type, click the “Save Changes” button at the bottom of the page. This will update your custom post URLs with the new structure.

It’s important to note that changing the permalink structure for your custom post type will also affect any existing URLs for that post type. This means that any existing links to your custom posts will no longer work unless you set up redirects.

Related: How to Start With a Custom WordPress Theme?

Conclusion

Rewriting your custom post URLs with taxonomy or category information can make your site more user-friendly and intuitive for your visitors. By using the Custom Post Type Permalinks plugin, you can easily customize the URL structure for your custom post types without having to write any code. Just remember to configure the permalink structure for each custom post type and save your changes to update your URLs.

Keep in mind that changing your custom post URLs can have implications for SEO, so it’s important to set up redirects and monitor your site’s performance after making these changes. With a little bit of planning and attention to detail, you can create a URL structure that works well for both your visitors and search engines.

Rate this post
Reviews & Ratings Get your stoe online with Shopify in 60 minutes Shop Now