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
I want to access two different types of taxonomies like city and room, how can I access it via wp permalink?
Great tutorial for custom taxonomies. I have found another one for Non-hierarchical Custom taxonomies.
Wow Amazing this customer taxonomy is great, I will give it a try and put my guest posts under this section.
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.
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
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!
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
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?
I have the same problem -_-
I’ve applied the code and its working. But all pages returns 404’s. How to fix this?
I have the same issue. Did you find the fixing?
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).
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;
}
This didn’t work for me unfortunately. 🙁
This worked for me – thank you!!!!!
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 );
Update permalinks. Go to settings > permalink and just save it.
Thanks . nice post i like
Thanks your Post helped us a lot creating a Rating System!
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?
http://shibashake.com/wordpress-theme/custom-post-type-permalinks-part-2#conflict
Suppose
I want to show texonomy in hierarchical format
and i want to patent and sub texonomies in permalink…
How to do that?
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
When I use this code does not work go to regular page. 404
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%/
It looks like a permalink conflict issue.
http://shibashake.com/wordpress-theme/custom-post-type-permalinks-part-2#conflict
How to implement this in a custom post type?
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!
It felt good to read your comment. Many thanks. 😀
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?
Sounds like a permalink conflict.
http://shibashake.com/wordpress-theme/custom-post-type-permalinks-part-2#conflict
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
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!
First off I want to thank you for writing this post. I’ve been researching this issue for days. It’ took my a long time to find this but so happy I finally did. Below I took your awesome function and made it fully dynamic. It should search through and replace as many un-translated customer slugs. I hope this helps everyone. Let me know what you think.
add_filter('post_link', 'rating_permalink', 10, 3);
add_filter('post_type_link', 'rating_permalink', 10, 3);
function rating_permalink($permalink, $post_id, $leavename) {
$matches = array();
$t = preg_match_all('/\%(.*?)\%/', $permalink, $matches, PREG_SET_ORDER);
// If no matches were found simply return the original permalink
if (empty($matches)) return $permalink;
// If matches were found translate them
// Get post
$post = get_post($post_id);
if (!$post) return $permalink;
foreach ($matches as $match) {
// Get taxonomy terms
$terms = wp_get_object_terms($post->ID, $match[1]);
if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0]))
{
$taxonomy_slug = $terms[0]->slug;
}
else
{
$taxonomy_slug = $match[1];
}
$permalink = str_replace($match[0], $taxonomy_slug, $permalink);
}
return $permalink;
}
hi admin,
acc. to your tutorial i added a unique tag into custom permalink structure. for example i added :- /%categoryProperty%/%postname%/.
but the permalink is still having post name only. /%categoryProperty%/ is not showing in permalink. can you suggest me what the wrong is happened?
I would look at the post_link and post_type_link function to see what is being passed in and what is being passed out.
Thanks a lot! I was afraid that this could ever be done. I built different permalinks for different depth:
hotels/europe/france/paris/a-post-about-a-hotel
and shallow itens such:
places/europe/france/10-parks-you-should-visit
places/europe/france/top-cheap-stores
I just needed to build the link:
$taxonomy_slug = $terms[0]->slug.'/'.$terms[1]->slug
Thanks a lot!
any idea how to add a username in a link?
for exampe. site url: http://mysite.com/
now adding a username makes it http://mysite.com/username
note. the username can be change e.g john, james or jones.
now for example we have a username john -> our url now is http://mysite.com/john
and if we visit other page like contact, the username should be ther on a link e.g http://mysite.com/john/contact.html
but if theres no username on the link. e.g http://mysite.com/
the contact page should only be http://mysite.com/contact.html
hopefully you could help me. cheers!
Probably can just use the %author% tag and then customize it as necessary.
Thanks, this was very helpful! Works like a charm!
I don’t follow this, I can’t tell what I’m suppose to leave the same and what I am suppose to replace with my custom post type name and taxonomy name.
I’ve tried this technique to help me migrate an old static-HTML web site to WordPress, without having to change any of the old page URLs. I created a custom taxonomy that corresponded to the directory structure of the old site. It works great, allowing me to give all my _posts_ the same URL that my static web pages used to have, except for one serious problem:
All of my _page_ permalinks no longer work.
Although this should not have affected page permalinks at all, it seems to have broken them. WordPress still thinks that they have not changed, but trying to go to them takes me to my 404 page instead. Does anyone have any ideas what might be wrong?
That is likely due to permalink conflicts.
http://shibashake.com/wordpress-theme/custom-post-type-permalinks-part-2#conflict
Okay, thanks, that makes sense. It’s not clear to me how to resolve this though, since I can’t just insert a unique slug into the structure (since my goal is to make the page URLs exactly the same as they were on the static site, which did not contain that unique slug).
Another solution is to just add a unique character instead of an entire slug, for example –
Note the underscore added to the beginning of the regex structure. I haven’t looked into this in great detail but playing around with the regex associated with each of the terms may bring some interesting solutions. I have tried the underscore solution on taxonomies, and it does work.
One can probably also change the regex structure for a ‘page’, but that is not something I have tried, so I don’t know how easy/difficult it is.
One (less than ideal) solution I just found is to make _all_ of my pages children of some (never displayed) parent page. This effectively adds a slug to the beginning of each parent page permaling, thus making it no longer regex-match my custom post permalink structure. Suggestions for a better idea are still appreciated.
Just found a fix that works!
This removes the rewrite rule that causes a bare page reference to be interpreted as a folder reference.
Stefan, you just saved my day. I was looking for that fix for more than 6 hours!
thanks for share!
Hi Stefan!
The one you posted works perfect but do you have a clue what would be the right rule for child pages?
I’ve tried it, it works perfectly, except for one thing: if the taxonomy is hierarchical, it only puts the first level in the URL. Can you tell me how to change the PHP so that it works on hierarchical taxonomies by having the path /parent/child/child/postname?
Thx!
You may need to traverse the parent relationship manually by using a loop.
First… thanks for your informative article.
I tried your suggestion but i get always an 404.
http://example.com/my-custom-post-type/taxo/child-of-taxo/post-title -> 404
If i delete the slug ‘child-of-taxo’ manually from the adressbar everything works fine.
http://example.com/my-custom-post-type/taxo/post-title -> 404
This the code i’m using:
add_filter('post_link', 'taxo_permalink', 10, 3);
add_filter('post_type_link', 'taxo_permalink', 10, 3);
function taxo_permalink($permalink, $post_id, $leavename) {
if (strpos($permalink, '%taxo%') === FALSE) return $permalink;
// Get post
$post = get_post($post_id);
if (!$post) return $permalink;
// Get taxonomy terms
$terms = wp_get_object_terms($post->ID, 'taxonomie');
if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) {
$slug1 = $terms[0]->slug;
$slug2 = $terms[1]->slug;
$taxonomy_slug = $slug1 . '/' . $slug2;
} else {
$taxonomy_slug = 'other';
}
return str_replace('%taxo%', $taxonomy_slug, $permalink);
}
I’m sure you are a busy man but i would appreciate a suggestion how to fix this problem. Do you have an idea?
I would try doing it with two separate tags – %taxo% and %childtaxo%
As before, Shibashake, I very much appreciate all your help and patience!
1) While that didn’t result in an error, the links did not function properly. Posts within a Taxonomy, and their URLs, worked perfectly. Posts within a Category worked perfectly, but their URLs removed the category entirely, leaving two slashes in the title (e.g.; site.com//postname/
2) I’ll have to dig further into this fix — or hire someone smarter than myself to help 😉 [I feel bad for hijacking your thread with my many comments, but if interested / available / affordable, I’d be honored if you took a stab! I sent you an email through your site with the same]
Hello Rob,
Sorry for the late reply. Have been busy with dog matters lately.
I am currently not working on external projects, but will be happy to answer whatever questions that I can. Good luck with your site.
no worries, shibashake – hope the dogs are well!! regarding my permalinks, i can’t thank you enough for getting me this far!! unfortunately, heading through the next steps is just a bit (a lot!) beyond my brain. i can’t quite get the “no taxonomy, then category” part working, and hooking into the request filter is an entirely foreign concept to me. if you had anything easy to further suggest, i’m happy to give it a try! but i don’t want to completely hijack your post with my dumbness 🙂
Hi Rob,
Your site looks very awesome!
If you send me the file you use to modify your permalinks, I can probably play around with it some to get the category thing working. I will send you an e-mail.
i recently created a “cartoons” taxonomy, with all different cartoon names as part of the group: pokemon, smurfs, thundercats, etc. most of my posts have a “cartoon” that they are associated with. however, there are some posts that are not part of a cartoon taxonomy.
without knowing the proper nerd speak (i’m only an unproper nerd), i’d love the permalink logic to act something like this:
if this post has a taxonmy, make the url:
/%cartoons%/%postname%/
else this post does NOT have a taxonomy, make the url:
/%category%/%postname%/
is that feasible using your code, above…? and if so, what would i enter in SETTINGS > PERMALINK ?
Hmmm, you could try using /%cartoons%/%postname%/ as your main permalink and then in the rating_permalink/cartooon_permalink function, insert in the post category instead of ‘not-rated’.
hi shibashake – first off, you’re the best 🙂
with that out of the way, i implemented as you detailed above, and it worked VERY well, but not perfectly. all of the posts within a cartoon taxonomy moved to their new URL without issue, which was awesome.
however, i hit two issues with the rest:
1) how do i change “else $taxonomy_slug = ‘no-rating’;” to force it to pull up the respective category slug? (as a quick fix, i’ve changed it to just the general term “cartoon”, which isn’t so bad!)
2) i’ve run into the 404 issue with pages. reading through the comments, i saw that you link people to: http://shibashake.com/wordpress-theme/custom-post-type-permalinks-part-2#conflict however, im hoping to avoid adding the extra portion of the slug (that’s the whole purpose of this change, for me, anyway). one page i have, for example, is “toonbarn.com/games” which results in the 404. even if i change the slug of that page to “toonbarn.com/gamessuperface” – which i did! 🙂 – i still get the 404 upon entry. is that still part of the same issue…?
[also, thanks for making me a hot girl! 🙂 ]
LOL! You are welcome. 😀
Hello Rob –
1. Try using get_the_category
http://codex.wordpress.org/Function_Reference/get_the_category
2. The 404 error occurs because 2 different objects share the same regular expression structure. Changing the actual name of the page or even changing the permalink tag names %my_taxonomy% will not solve the issue because the regular expression structure is still similar.
You can quickly identify if it is a regular expression structure issue by adding in the slug and seeing if that solves the problem. Alternatively, you can hook into the ‘request’ filter and see what arguments are being passed into the query. There is an example of the request filter here –
http://shibashake.com/wordpress-theme/mastering-the-wordpress-loop
thanks again for the help, shibashake
1) unfortunately, i couldn’t get this to work. i’m likely just not smart enough to get the proper php phrasing when replacing the “else $taxonomy_slug” with something like “get_the_category”. The closest I could get was the URL coming up with the term “Array” as that portion of the slug. In the meantime, seems like it’s just easiest for me to keep the “fake taxonomy” of just the simple “cartoon”
2) as you said, I tried adding another term to the front of the permalink structure, and it had everything working again! the pages AND the posts with the new custom URL. Now that it is identified as an expression structure issue… does that mean its possible to bypass? I really only have three or four pages, so I’d love to be able to get to everything without the extra slug piece (though it wouldn’t be the end of the world if it had to be)
Hello Rob,
1. Try something like –
instead of else $taxonomy_slug = ‘not-rated’;
2. You could solve the 404 issue by making the regular expression structure unique. For example we could add unique characters into the taxonomy name. Another possibility is to hook into the request filter and resolve it manually there. Both are fairly complex though.
Hi,
Having difficulty making this work for a hierarchical category structure. Would you consider working with me on this as a paid task?
Basically I have 2 custom post types: ecp and clw (in short), each with a ‘product type’ taxonomy, and in each single custom post URL I need to add the category slug, whilst leaving the standard wordpress ‘Blog’ post URLs in tact.
Thanks
David
Hello David,
Sounds like it may be a permalink conflict issue.
http://shibashake.com/wordpress-theme/custom-post-type-permalinks-part-2#conflict
Two ways to debug permalink conflicts –
1. Add a unique keyword (e.g., ‘clw’) into the custom post type permalink, e.g. //mysite.com/clw/product_type/product_name.
2. Hook into the request filter and see which rewrite rule has fired and which query variables are being passed to the wordpress loop.
This WordPress Loop article has an example of the request filter.
Thanks for this. As I mentioned, would you be interested in quoting to deploy the correct permalinks onto the site for me?
Thanks
David
Hello David,
At the moment, I am not taking on any external projects.
In terms of the permalinks, it is possible to have multiple objects share the same permalink rule, but you would have to resolve them yourself within the request filter function and that could get a bit messy. Usually it is much easier to just insert in a unique keyword and that would solve the permalink conflict issues.
Hi, thanks for this. It’s not actually the conflict that’s the problem (I inserted an additional keyword) just cannot get the permalinks to appear correctly at all. In the custom post types I have set the taxonomies to REWRITE and have the slug left blank – is this correct?
Thanks again
David
I can probably give the code a quick look through if you want. Just post or email me the link.
http://shibashake.hubpages.com/_srec/contact
Hey many thanks on the post, truly helpful.
I did run into problems however. I have a page with /current/ and /current/members/
And a custom taxonomy for state. (eg: /current/members/state/)
Next I want to do a custom post type with the members name in it (/current/members/%state%/%member_name%/).
While testing I noticed that when I set my custom post type slug to /current/members/%member_name% it works fine without the %state%, but when I added it in, I get a page not found error.
I read your permalink conflicts and this articles and the comments, but I could not get much info to how to solve this. Is there anything I missed? If it is the permalink conflict, how do I debug?
It would depend on whether your slugs ‘/current/members/’ are unique to just a single post type. If you are using it for multiple post types, then there will likely be conflicts.
To debug, try hooking into the ‘request’ filter and see what query arguments are being passed on. This will show which permalink rule actually fired, and why you are getting a 404.
Hey, many thanks for the comprehensive tutorials! Your site has been by far the only place where I’ve found good info on the exact subjects I’ve been looking for – your articles about custom columns and input panels have been great.
However, I’ve been having a problem getting my permalinks to work as I want them to. I hope you find my problem to be an interesting puzzle, and perhaps help me out with finding the solution.
I have a custom post type, ‘songs’, and taxonomy, ‘bands’. I want my URLs to be of the format /lyrics/%bandname%/%songname%. Two different songs can have the same title, so if I have two different posts – “Song 1” by “Band 1”, and “Song 1” by “Band 2” – /band1/song1 and /band2/song1/ would point to them. However, one song can also be covered by multiple artists, so if I have a single post called “Song 1” tagged with both “Band 1” and “Band 2”, both permalinks would redirect to the same post.
I think that this is perfectly possible with hierarchical posts and taxonomies. However, the exact details of the implementation have eluded me for a while. Maybe you could provide me with a few pointers? Sorry if I’m asking too much, I really want to get this out of my hair.
Hello Adam,
It sounds like what you describe is more of a parse_request or parse_query issue. As long as the songname is unique, I think both permalinks will work. If you have problems, then hook into the ‘request’ filter and check out what is being passed in the query arguments. There is an example of the request filter here –
http://shibashake.com/wordpress-theme/mastering-the-wordpress-loop
The tutorial outlined here is for generating the proper permalinks for a particular songname. In particular, the permalink generated will only include a single band, e.g. Band 1.
Is there a way to use this with hierarchical taxonomies?? For example, if I had a taxonomy of release_type that had the following structure:
DVD
-Region 1
-Region 2
etc…
Would it be possible with a variation of your code?
and I wanted the permalinks to be as such:
site.com/dvd/region-1
Yeah, you can replace your tag with anything you want. However, there may with permalink conflict issues with that permalink structure-
http://shibashake.com/wordpress-theme/custom-post-type-permalinks-part-2#conflict
I have a traveling blog where I post images and video of the place I visit. I want my blog to have a structure like http://domain.com/2011/new-york/my-trip.html
2011 – I need it for WordPress to work faster with the databse
new-york – that’s like the category, although I think it should be a taxonomy?!
my-trip.html = that’s my post name
I also want to have posts identified as videos/photo gallery/hotel review, etc.
What should I use (custom posts? , taxonomies?) I also want the best structure (permalink) for my URL.
please help me out, working days on this with no results yet.
Thanks in advance guys.
Hello AskerGuy,
It seems that you can achieve what you want with just existing post_tags and categories. However, it is difficult to say for sure without knowing all the details.
I have a question, I am using custom taxonomies on my website, which works fine, but is it possible to have custom taxonomies tag or in other words the entire permalink structure on my title tag?
Is it possible to have my custom permalink structure on title tag of my blog?
my current permalink structure is this
`/%postname%/%location%/%mba_courses%/`
where my location and mba_courses are custom taxonomies
Now I want this on my title tag, can I write something like this in title tag?
“
I know this is a wrong code, and it won’t work, but is their any way, where I can have this kind of title tag?
This would likely depend on your theme templates. Look in your theme templates for the title tags and then you can modify its contents to whatever you want.
I didn’t get you,
I have my current URL in this format
/%postname%/%location%/%mba_courses%/
which means
/mba-in-insead-university/mba-in-france/mba-in-accounts/
So same I want in my title tag, so therefore, it will be something like this:-
MBA In Insead University | MBA In France | MBA In Accounts
Which is same as the URL, so how can I get this
remember in permalink location and mba_courses are my custom taxonomies
hey how can I do this?
I am really stumped
Noob here Trying to wrap my head around add_action and add_filter… what PHP files are we to be placing this code into?
Two common places –
1. Your theme functions.php file. This file is commonly found in …/wp-content/themes/[theme directory]/functions.php
2. The main plugin file. This file is commonly found in ../wp-content/plugins/[plugin slug]/[plugin slug].php
Very cool, nice work. Thank you much!
Just noticed that using your code exactly as you have it, kills my pages for some reason. Posts work fine, just pages throw a 404 error. Any ideas?
Sounds like a permalink conflict issue –
http://shibashake.com/wordpress-theme/custom-post-type-permalinks-part-2#conflict
Brilliant! Just what I was looking for. Nice work on all the wordpress customizations, much appreciated.
Thanks for the great article. I was wondering if there is some way to do this for categories too. I am building a site which has the same categories for several different cities. I have created a custom taxonomy called city, and have successfully gotten posts to use the structure:
%city%/%postname%
I would also like this for categories, so I could have
%city%/%category%
For example,
domain.com/new-york/food/fine-dining
would return all posts categorized as ‘fine-dining’ that also have the city term=’new-york’ (and fine-dining is a subcategory of food).
Thanks in advance for any advice.
Hello Robert,
I haven’t done this before with categories, but it should definitely be very doable.
You can probably replace the existing permalink structure of categories with $wp_rewrite->add_permastruct. Something like this –
$wp_rewrite->add_permastruct(‘category’, $category_structure, false);
Here is where taxonomy permalink rules get added in the WordPress source –
http://phpxref.ftwr.co.uk/wordpress/nav.html?wp-includes/taxonomy.php.source.html#l325
Great article! In fact, you have lots of great articles on WordPress.
I followed your method and my custom permalinks for several custom post types are working great. they replace %artist% in my URL with an artist name. However, whenever I try to view a page I get a ‘page not found’ error.
Also, same thing happens for normal posts. I can get posts working if I set my permalink settings to /%artist%/%postname% but if I leave out %artist% I get a ‘post not found’ error.
Any ideas where the conflict is occurring for pages and posts?
Cheers
Hello Dave,
What you describe sounds like the dreaded permalink conflict issue. I have added a section to the main permalink article that explains this problem –
http://shibashake.com/wordpress-theme/custom-post-type-permalinks-part-2#conflict
It seems that permalink conflict issue was the culprit.
I added a unique name to the beginning of each custom post type permalink structure (as advised by your article) and I can now view posts (normal and custom) and pages without any problems.
Cheers for the help.
Keep up the good work, your site is the best resource for tricky WordPress issues!
Hi there,
There is a small typo in the following code:
`add_filter(‘post_type_link’, ‘rating_permalink’), 10, 3);`
Should be:
`add_filter(‘post_type_link’, ‘rating_permalink’, 10, 3);`
I removed a parenthesis.
I have corrected it. Thanks for letting me know. 😀
I’m having an issue here too. I have adapted your code for my two taxonomies: ‘artist’ and ‘writer’. Also, I use a custom post type ‘story’.
When I define the rules to /%artist%/%writer%/%postname% or /%artist%/%writer%/%story% I get a not found.
In general, I find that it is best to go slowly –
1. First, make sure the custom post type permalink is working, i.e. don’t include a taxonomy tag yet.
2. Then only include one taxonomy tag.
3. Once that works, add another one and so on.
At this point, it is difficult to tell whether the issue is with the custom post type or with the taxonomy. To debug, you can also hook into the ‘request’ filter and see what the query arguments are.
Hi there, first, nice work! I learn some cool tricks with your tutorials. But can you help me? I try almost 2 days to get this structure to working:
For custom post types:
/%category%/%child_category/%post_type%/%postname%/
/%category%/%post_type%/%postname%/
For normal posts:
/%category%/%child_category/%postname%/
/%category%/%postname%/
But no lucky. I’m using the default wordpres “categories” as taxonomies for posts and other post_types. But when I got the permalink right on admin, my template get 404.
Thanks,
homem robo
For custom post types you need to use the custom post type tag. For example, if my custom post type = gallery, then I do
/%category%/gallery/%gallery%/
If you want to use your own tags, e.g. %child_category% or %post_type% you must first define them. Permalink tags can get created during taxonomy or custom post type creation, or you can create them yourself using add_rewrite_tag.
Here is an article specific to setting the permalinks for custom post types –
http://shibashake.com/wordpress-theme/custom-post-type-permalinks-part-2
I am designing a website for a newspaper. I used this tutorial to get the article permalinks to be http:…/article/[issue-date]/[section]/[article-headline]. It worked for a while but broke a few days later when I was working on something unrelated. I changed my code back but it still doesn’t work. It changes the permalinks correctly and the links lead to the correct place but I get a 404 page. Any ideas what could be wrong?
Sorry for posting again but I forgot to add an observation I had made. I found it pretty odd that it could somehow get the correct article information but not find the article. I mean it needed the article in the first place to get the proper taxonomies, right?
My guess is that there is a conflict with some other permalink structure that you are using. If there is a conflict (i.e. two permalink rules that match the same address string), then only the first rule that matches will fire.
It could also be that the permalink rules weren’t properly flushed before. Every time you change any permalink structure you want to flush your permalink rules so that you are sure that old permalink structures are fully cleared.
I flush the permalinks about every 30 sec (sometimes twice after I make changes just to make sure) and I don’t know what other permalink structure could be conflicting. I even restarted my computer multiple times since it happened and manually cleared out whatever caches I could find. It is not just a browser specific problem though.
I honestly don’t remember anymore what changes I made when I first encountered this problem but the first thing I did when it happened was to revert the changes. Upon researching I saw a lot of people mention the .htaccess file, could there maybe be conflicting rewrite rules there because I can’t think of anywhere else where I have played with rewrite rules.
I don’t think it is a conflicting name problem as the whole reason I created this long permalink structure was to avoid conflicts and also it happens with all articles…
Try linking into the ‘request’ filter and print out the query arguments when you access the 404 page.
For example –
This will help you to identify which rewrite rule got fired and why the page did not get retrieved.
Thanks for the suggestion, I had been looking for some way to debug the request. I did it though and only got this output:
(condensed to one line for space) Not sure where do go from there. Does this mean that it is directly calling a 404 error or that the problem is happening before the call. The site is locally hosted so unfortunately I can’t provide an address to show you but if there is any code or settings that might help I would be happy to provide them. Thanks for the attempt though.
Oh and would it make any difference what my normal post rewrite rules are set to? I have /%postname%/%post_id% now, which works for normal posts and printed out the right info from the code above in a comment.
Sorry for the number of comments but I just noticed that in the above comment the output was cut off. Basically I got an array with “[error] => 404” in it.
Hmmm, that seems to indicate that none of the rewrite rules fired. Is article-headline a custom post type? It could be an issue with the rewrite tag or an issue with the add_permastruct call.
At this point I would just try the taxonomy tags in your regular posts just to make sure that they work. Then I would test out your custom post type permalinks with the regular WP structure. Then test it out with your own structure but with no taxonomy tags. Once that is all working, slowly add in 1 taxonomy tag at a time.
Well Article is the custom post type, article headline was what I was calling the title of the article. The odd thing is that it worked for a while so I know that it is possible to do. I will try adding the taxonomies back into regular posts and then seeing if it works with them.
I feel like although it seems to not be firing the rewrite rules, it also seems like it has to be doing it. It changes the strings everywhere. When I go to edit an article the permalink displayed at the top will have the changes. All the links have the changes.
I’ll keep looking through and try out your suggestions and post back whenever I have anything. Thanks.
I just realized that I forgot to include an important (I think at least) detail. I mentioned that I am dealing with the article post type but the rewrite slug of article is set to “article%issue%%section%” with issue and section being replaced. I did it that way so that if for some reason an article does not have an issue or section it replaces it with “” and if it does it replaces it with “/{whatever}.” This worked for a while so I know the method isn’t what is messing up.
You saved me a lot of time! thanks a lot for this !
The taxonomy retrieval function is what I don’t understand.
Does it work the same as when you are retrieving taxonomy values on a single post page?
e.g.
Yeah you can do that. Now you just need to create a string out of the terms so that you may replace it into your permalink string at the %speaker-name% tag location. For example something like this –
Sorry I didn’t reply on the previous comment – feel free to move it if you want to keep the conversation context.
So this is what I have:
Do I need to specify any parameters in my custom post type as it relates to the rewrite value? With the above code I tried this with the rewrite value:
But the permalink comes back as: wp-admin/none
When I don’t add any rewrite parameter nothing changes in the permalink.
Any ideas?
Thanks for all the help
Also your podcast rewrite permalink should be something like
Wonderful! Works great with a couple exceptions.
First – when you hover over the view link when looking a list of posts in the admin it doesn’t fill in the taxonomy it defaults to the ‘else’ value.
Second – the same thing happens when you query the posts for display on the front end and declare that the ‘post_type’ => ‘podcast’. If you don’t specify the post_type in your query the permalink comes up fine.
I’m assume the admin is also querying the posts and specifying that the type is podcast, so the two issues are most likely related.
That’s not a big deal for me at this point but its definitely not ideal.
Anyway, thanks for the help. I really appreciate it.
That sounds strange. So in both cases it is showing up as /no-speakers/test-1?
Two things that come to mind –
1. Make sure to flush your rewrite rules.
2. Do some echos in your function and see what terms are being retrieved by wp_get_object_terms.
Yep. Whatever value is assigned to ‘else’ shows up in the permalink in those two instances.
I have flushed my rewrite rules by both visiting the permalinks page and by changing the default permalinks but neither helped.
When I echoed out the terms it retrieved the correct object and displayed it fine.
I then did some additional investigating and found another post on the subject: http://xplus3.net/2010/05/20/wp3-custom-post-type-permalinks/
Comparing your code and his it looks like you have to declare the post_type so when you query it brings back the correct object.
Doing that solved both issues!
Thanks for the help and teaching me something new.
Thanks for all the different articles on changing up the permalinks.
I’m a little fuzzy however trying to figure out how to enter my own function to retrieve a specific taxonomy value.
I have a custom taxonomy called ‘speaker-name’ with the values of different speakers.
I have a custom post type called ‘podcast’. I want the permalink to include the name of the speaker which is checked in the ‘speaker-name’ taxonomy.
Thanks!
You want to hook into the post_type_link filter then.
Then just pattern your podcast_permalink function similar to the rating_permalink example I showed above. In line 10 is where you want to put in your own taxonomy retrieval function. Then make sure to replace the %rating% tag with %speaker-name%.