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 Cancel reply

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

Comments

« Previous 1 2 3 4 5 6 Next »
  1. Shakti Patel says

    November 25, 2014 at 3:13 am

    Thanks . nice post i like

    Reply
  2. Bine says

    April 6, 2014 at 2:05 pm

    Thanks your Post helped us a lot creating a Rating System!

    Reply
  3. Ilan says

    February 19, 2014 at 5:17 pm

    I hope this post is still active and i can get some advice.

    I successfully implemented the taxonomy into the rewrites however now my pagination doesn’t work.

    When i go to page 2 i get a 404.

    Any ideas why?

    Reply
    • ShibaShake says

      February 19, 2014 at 10:24 pm

      http://shibashake.com/wordpress-theme/custom-post-type-permalinks-part-2#conflict

      Reply
      • parth says

        February 21, 2015 at 6:07 am

        Suppose
        I want to show texonomy in hierarchical format

        and i want to patent and sub texonomies in permalink…

        How to do that?

  4. Robbert says

    January 21, 2014 at 4:03 am

    Hi ShibaShake,
    Thanks a lot for this article. This is almost exactly what I was looking for. You explain how you can add the tag %rating% in WordPress Admin permalinks. But If I want to use this structure only for 1 specific post type, how can I do that?

    Thanks!
    Robbert

    Reply
  5. ManiMills says

    June 10, 2013 at 2:20 am

    When I use this code does not work go to regular page. 404

    Reply
    • ManiMills says

      June 10, 2013 at 2:28 am

      function gl_cat_permalink($permalink, $post_id, $leavename) {
      if (strpos($permalink, ‘%gl_category%’) === FALSE) return $permalink;

      // Get post
      $post = get_post($post_id);
      if (!$post) return $permalink;

      // Get taxonomy terms
      $terms = wp_get_object_terms($post->ID, ‘gl_category’);
      if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) $taxonomy_slug = $terms[0]->slug;
      else $taxonomy_slug = ‘not-gl-categoies’;

      return str_replace(‘%gl_category%’, $taxonomy_slug, $permalink);
      }

      /%gl_category%/%post_id%/%postname%/

      Reply
    • ShibaShake says

      June 10, 2013 at 11:47 am

      It looks like a permalink conflict issue.
      http://shibashake.com/wordpress-theme/custom-post-type-permalinks-part-2#conflict

      Reply
  6. James says

    June 2, 2013 at 1:03 pm

    How to implement this in a custom post type?

    Reply
  7. Code Is Poetry says

    April 22, 2013 at 7:17 pm

    Thank you very much for your post!
    You are the only site on the web that explains this subject clearly.
    I followed though all your example, and I successfully managed to create a custom taxonomy
    that allowed me to use multiple hierarchy in the url [It was a bit too hard, but without this it would have been impossible].
    By the way, your 3D arg is trully amazing. I like all your characters and your style. You are surely a very talented person.
    Thanks!

    Reply
    • ShibaShake says

      April 22, 2013 at 9:42 pm

      It felt good to read your comment. Many thanks. šŸ˜€

      Reply
  8. David - DiseƱo Web says

    September 28, 2012 at 3:04 pm

    Hello! Thanks for your advice… I get the same error of ZAK, and a I get error 404 in pages.

    I used this code….

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

    function provincia_permalink($permalink, $post_id, $leavename) {
    if (strpos($permalink, ‘%provincia%’) === FALSE) return $permalink;

    // Get post
    $post = get_post($post_id);
    if (!$post) return $permalink;

    // Get taxonomy terms
    $terms = wp_get_object_terms($post->ID, ‘provincia’);
    if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) $taxonomy_slug = $terms[0]->slug;
    else $taxonomy_slug = ‘general’;

    return str_replace(‘%provincia%’, $taxonomy_slug, $permalink);
    }

    I used the my_blog_reques function, but it gets in a page [provincia]=>….

    I don’t know why it gets the page rule and where it is.

    Regards. Thanks.

    Would you help me?

    Reply
    • ShibaShake says

      September 28, 2012 at 5:22 pm

      Sounds like a permalink conflict.
      http://shibashake.com/wordpress-theme/custom-post-type-permalinks-part-2#conflict

      Reply
  9. avi says

    September 4, 2012 at 1:07 am

    hello

    i see your post while searching for some solution, i am not wp expert not programming geek, but i need solution for my blog
    my question is – i want to create a new url structure with my categories and custom taxonomies,
    like example

    Custom Taxonomy “programs” with child taxonomies, fine art, communication design, graphic design, printmaking.

    In the main menu under the parent category “News” the child categories are, student news, faculty news, and alumni news
    now i want to show the student news for fine arts
    site.com/student-news/fine-arts/ and
    site.com/faculty-news/fine-arts/ and
    site.com/faculty-news/printmaking/ and
    site.com/alumni-news/fine-arts/ and so on

    can you help me please.
    many thanks

    Reply
  10. Hasan Khan says

    August 19, 2012 at 7:08 am

    I found this post while trying to modify the permalink structure for my new blog, but can’t make sense of it as I’m a PHP newbie. I bought a review theme, and it has custom post type “reviews” built in as well as categories for those reviews. The current permalink for reviews is “/reviews/postname/” however, I want it to be “/reviews/category/postname”.

    My category permalinks are “review-cats/category” but I want them to be just “reviews/category” and my tags permalinks are “review-tags/tag” and I want them to just be “reviews/tag”. Any idea how I can accomplish this? It would be AMAZING if you could help me out!

    Reply
« Previous 1 2 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 ·