Shiba

Adventures in WordPress

  • Home
  • Dog
  • Art
  • Contact
  • WordPress Articles
    • WP Plugins
    • WP Programming
    • WP Admin Panels
    • WP Theme Design
    • WP How-To
    • WP Theme Images
You are here: Home / WordPress Admin / Custom Taxonomy / Add Custom Taxonomy Tags to Your WordPress Permalinks

Add Custom Taxonomy Tags to Your WordPress Permalinks

by ShibaShake 125 Comments

The WordPress custom taxonomy system allows you to create your own grouping of WordPress objects (e.g. posts, pages, and custom post types). Tags and categories, for example, are taxonomy objects that are native to the WordPress system. With the custom taxonomy framework, you can create your own tags and categories.

In the article Custom Post Type Permalinks (Part 2), Lane and Andrew asked the very good question of how we can add custom taxonomy tags to WordPress permalinks.

For example, if we have a custom taxonomy called rating we might want to set our WordPress permalink structure to –

/%rating%/%postname%

Here we consider how to insert custom taxonomy tags into the WordPress permalink structure.

1. Create a Custom Taxonomy

First, we create a custom taxonomy object called rating with the register_taxonomy WordPress function.

add_action('init', 'my_rating_init');

function my_rating_init() {
	if (!is_taxonomy('rating')) {
		register_taxonomy( 'rating', 'post', 
				   array( 	'hierarchical' => FALSE, 'label' => __('Rating'),  
						'public' => TRUE, 'show_ui' => TRUE,
						'query_var' => 'rating',
						'rewrite' => true ) );
	}
}

Setting the rewrite argument to true (Line 10) will automatically add the tag %rating% to our WordPress system.

However, if we try to set our blog permalink structure to –

/%rating%/%postname%

Our new post permalinks will look like this

http://shibashake.com/wordpress-theme/%rating%/test-1

As a result we will get a 404 file not found error. This is because our %rating% tag was not properly translated.

2. Translate Our Custom Taxonomy Tag

To translate our new %rating% tag, we must hook into the permalink generation function – get_permalink.

The post_link hook allows us to translate tags for regular post objects and the post_type_link hook allows us to translate tags for custom post type objects.

Note that both filter hooks accept exactly the same arguments, so we can tie both hooks to the same function.

add_filter('post_link', 'rating_permalink', 10, 3);
add_filter('post_type_link', 'rating_permalink', 10, 3);

function rating_permalink($permalink, $post_id, $leavename) {
	if (strpos($permalink, '%rating%') === FALSE) return $permalink;
	
        // Get post
        $post = get_post($post_id);
        if (!$post) return $permalink;

        // Get taxonomy terms
        $terms = wp_get_object_terms($post->ID, 'rating');	
        if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) $taxonomy_slug = $terms[0]->slug;
        else $taxonomy_slug = 'not-rated';

	return str_replace('%rating%', $taxonomy_slug, $permalink);
}	

Line 5 – If the permalink does not contain the %rating% tag, then we don’t need to translate anything.
Line 12 – Get the rating terms related to the current post object.
Line 13 – Retrieve the slug value of the first rating custom taxonomy object linked to the current post.
Line 14 – If no rating terms are retrieved, then replace our rating tag with the value not-rated.
Line 16 – Replace the %rating% tag with our custom taxonomy slug.

Our new post permalinks will look like this –

http://shibashake.com/wordpress-theme/rated-a/test-1

Leave a Reply to David Hedley Cancel reply

Your email address will not be published. Required fields are marked *

Comments

« Previous 1 … 3 4 5 6 Next »
  1. Darshan Saroya says

    June 17, 2018 at 9:03 am

    I want to access two different types of taxonomies like city and room, how can I access it via wp permalink?

    Reply
  2. Owais says

    August 10, 2017 at 1:38 am

    Great tutorial for custom taxonomies. I have found another one for Non-hierarchical Custom taxonomies.

    Reply
  3. My site plan says

    August 7, 2017 at 11:01 pm

    Wow Amazing this customer taxonomy is great, I will give it a try and put my guest posts under this section.

    Reply
  4. Haripal says

    December 28, 2016 at 9:53 pm

    Thanks ShibaShake for sharing this article, I have added the code for custom post type for taxonomy links in permalink. For costom post type it works fine, But when i check pages and posts the permalink is not working shows 404 error. Please give reply how to solve this issue.

    Reply
  5. David says

    November 10, 2016 at 1:10 am

    thanks for your code really solved lots of my permalink structured, could if be possible for me to print /%rating%/ using conditional statement like
    [code]
    if(!empty(rating)):

    echo "/%rating%/ ";

    [code]

    instead of printing not-rated. thanks

    Reply
  6. Carlos says

    November 3, 2016 at 7:54 am

    Awesome!

    I did not know that just adjusting those settings it could also be used!

    I use CustomPress for custom post types management, and I just needed to see how to use my custom taxonomy in the URL.

    To think that I just had to use it like that, %custom_taxonomy_name%!

    You are such a life saver, thanks!

    Reply
  7. Javier says

    May 22, 2016 at 2:03 am

    When you use the same rewrite as the custom taxonomy, it works for posts, but not for pages like domain.com/archive or domain.com/search

    If your slug for custom taxonomy is at this example “rating”, it will work

    Reply
  8. Marc Getter says

    May 17, 2016 at 5:48 pm

    It looks like with 4.5.2 this no longer works.

    This is my code:

    add_filter(‘post_link’, ‘locale_permalink’, 10, 3);
    add_filter(‘post_type_link’, ‘locale_permalink’, 10, 3);

    function locale_permalink($permalink, $post_id, $leavename) {
    if (strpos($permalink, ‘%locale%’) === FALSE) return $permalink;
    // Get post
    $post = get_post($post_id);
    //print_r($post_id);
    if (!$post) return $permalink;
    // Get taxonomy terms
    $terms = wp_get_object_terms($post->ID, ‘locale’);
    if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) $taxonomy_slug = $terms[0]->slug;
    else $taxonomy_slug = ‘no-locale’;
    return str_replace(‘%locale%’, $taxonomy_slug, $permalink);
    }

    I’m using /%locale%/%postname% as the custom permalink.

    The urls are rewriting fine, and if I check the post id, it’s returning the correct value. The problem is that wordpress is returning a 404 whereas this worked before upgrading to 4.5.2. Before I go through the process of downgrading back to 4.4, is there something I’m missing?

    Reply
    • Bree says

      May 26, 2016 at 12:16 pm

      I have the same problem -_-

      Reply
  9. Remko says

    August 11, 2015 at 10:22 am

    I’ve applied the code and its working. But all pages returns 404’s. How to fix this?

    Reply
    • Saulo says

      August 20, 2015 at 6:08 pm

      I have the same issue. Did you find the fixing?

      Reply
      • JB says

        March 25, 2016 at 8:11 am

        You need to modify the request due to the permalink conflict.
        I am doing an explicit look for my custom tag values and if I don’t find them then I’m adding in the page_id and post _id using a custom function I built to get the if from the urls slug (which WP does not have a function for).

    • Saulo Padilha says

      August 20, 2015 at 7:27 pm

      This code on functions.php fixed for me.

      add_filter(‘rewrite_rules_array’,’remove_bare_folder_rule’);
      function remove_bare_folder_rule( $rules )
      {
      unset($rules[‘([^/]+)/?$’]);
      return $rules;
      }

      Reply
      • JB says

        March 25, 2016 at 8:11 am

        This didn’t work for me unfortunately. 🙁

      • Anonymous says

        April 3, 2017 at 5:32 pm

        This worked for me – thank you!!!!!

    • Jesse says

      February 12, 2016 at 4:37 pm

      Try adding your replacement tag with add_rewrite_tag(), eg. using the example ‘%rating%’:

      function rating_rewrite_tag() {
      add_rewrite_tag( ‘%rating%’, ‘(.+)’, ‘rating=’ );
      }
      add_action( ‘init’, ‘rating_rewrite_tag’, 10, 0 );

      Reply
    • Darshan Saroya says

      June 17, 2018 at 9:06 am

      Update permalinks. Go to settings > permalink and just save it.

      Reply
« Previous 1 … 3 4 5 6 Next »

Recent Posts

  • Screenshot of an example article in code view of a modified Gutenberg editor.How to Harness the Power of WordPress Gutenberg Blocks and Combine It with Legacy Free-Form Text
  • Screenshot of the Success, WordPress has been installed page.Migrating Your WordPress Website to Amazon EC2 (AWS)
  • Screenshot of WinSCP for creating a SFTP configuration.How to Set-Up SFTP on Amazon EC2 (AWS)
  • WordPress Gutenberg code view screenshot of this article.How to Prevent Gutenberg Autop from Messing Up Your Code, Shortcodes, and Scripts
  • Screenshot of the Success, WordPress has been installed page.How to Create a WordPress Website on Amazon EC2 (AWS)

Recent Comments

  • Screenshot of the Success, WordPress has been installed page.How to Create a WordPress Website on Amazon EC2 (AWS) (1)
    • Erik
      - Great article. All worked great except for this step:apt install php-mysqlChanging to this fixed it:apt install ...
  • Add Custom Taxonomy Tags to Your WordPress Permalinks (125)
    • Anthony
      - Where does this code go? Like, what exact .php file please?
  • Screenshot of an example article in code view of a modified Gutenberg editor.How to Harness the Power of WordPress Gutenberg Blocks and Combine It with Legacy Free-Form Text (1)
    • tom
      - hi,my experience was like the same, but for me as theme developer the "lazy blocks"(https://wordpress.org/plugins/lazy-blocks/) ...
  • WordPress Custom Taxonomy Input Panels (106)
    • Phil T
      - This is unnecessarily confusing. Why declare a variable with the same name as your taxonomy? Why did you choose a taxonomy ...
  • Create Pop-up Windows in Your WordPress Blog with Thickbox (57)
    • Jim Camomile
      - I have used this tutorial several times and it is one of the few simple and effective ways to add popups with dynamic content, ...

Copyright © 2025 · Genesis Skins by ShibaShake · Terms of Service · Privacy Policy ·