65 Responses

  1. scribu
    scribu Published |

    I have a plugin that creates many-to-many relationships between posts:

    http://wordpress.org/extend/plugins/posts-to-posts/

    I take a different approach: There’s a single, hidden, taxonomy. The posts are linked to terms through the post ids, avoiding the problems you mention in Option #1. The user just chooses which post type should connect to which.

    With the movie example, each taxonomy in your example would become a custom post type. So you would have a ‘producer’ post type etc. This has advantages and disadvantages.

    Reply
    1. baju rajut murah
      baju rajut murah Published |

      Awesome Your weblog offered us useful info to work on

      Reply
  2. Derek Perkins
    Derek Perkins Published |

    I’ve been using scribu’s plugin for a while now and it works great for me.

    Reply
  3. Francois
    Francois Published |

    I did use a simple custom field to link them between the post and the taxonomy. The value of my custom field is the tag name.

    Post template : Link to the tag with the custom field value
    Taxonomy template : I search for the post custom field where the value equals the taxonomy I’m on

    But with this method, you need a little bit of manuel work, by entering the custom field, but it’s working great !

    Reply
  4. paul
    paul Published |

    I think there’s a typo here :
    * Movie (post type).
    * Person (post type).
    * Actor (post type).

    should be
    * Actor (taxonomy).
    ?

    Reply
  5. Iva
    Iva Published |

    Justin, thank you. I have managed to define the structure of a couple things and I stopped when I got to this. I somehow knew you’ll have a solution that I can actually understand (written for us designers, too, not just programmers…), so I’m glad I waited for a couple more days instead of asking someplace else.

    The only thing I still cannot get is how to make internal connections among terms from two different post types and related taxonomies. Say, there’s post type called “performance” and it’s basically a band or artist concert or concert cameo. If I wanted to list all songs played on that show, assuming they have their own post type, “song”, is there a non-manual way of achieving that?

    (A close example of this would be what’s done on Pearl Jam official site – http://www.pearljam.com/tour/show/alges-jul-10-2010 – and now I’m laughing at how they amade their term archives publically available…)

    I am sorry if this question is waaay out there.

    Reply
  6. shawn
    shawn Published |

    The complex part would be the UI. This would make for a really cool tutorial though.

    +1 on the tutorial!

    Especially dealing with the scenario of editing a performance page where you are adding songs, but have to add a song that does not yet exist.

    Currently you would have to leave the performance admin edit screen, go and add the song, and then return to the performance screen in order to add it.

    Very very tedious, and yet a problem that I have not been able to solve for months now. (editing or creating one post type within another post type admin)

    Reply
  7. Andrew
    Andrew Published |

    Another great and useful tutorial. This may not be the perfect answer, as you’ve said but we still owe you a big thank you for sharing it to us.

    Reply
  8. serge
    serge Published |

    You don’t mentioned it, there is a plugin called taxonomy metadata that give us the ability to create, delete, update term medata : http://wordpress.org/extend/plugins/taxonomy-metadata/

    Reply
  9. pedro
    pedro Published |

    Great tutorial,

    but how does one, when using sitemaps and tags, allow a tag page to show all tags used across multiple post types. Currently my site has ‘news’ and ‘film reviews – as the standard wordpress posts’ however my site uses tags, which when clicking on them for the custom post type return error pages however they work for the standard post type.

    essentially i’m looking for searches, sitemaps, tags and other plugins to search for all content regardless of post type.

    thanks

    Reply
    1. Michael
      Michael Published |

      Hey Pedro –

      On one of my sites, I have five custom post types but don’t use the “Post” post type. I had to have my author archives show all the posts across those five post types – by default it only showed “Posts”, which was empty. It sounds like that’s your problem too, so here was my solution, which I placed in functions.php:

      add_filter( 'pre_get_posts', 'show_all_author_posts' );
      
      function show_all_author_posts ( $query ) {
      	
      	if ( is_author() )
      		$query->set( 'post_type', array('editorial', 'interview', 'contest', 'movie', 'dvd') );
      	return $query;
      	
      }

      Just try changing is_author() to is_tag() and replacing my post types with your own.

      Reply
    2. Victor Teixeira
      Victor Teixeira Published |

      I have the solution to this:


      function post_type_tags_fix($request) {
      if ( isset($request['tag']) && !isset($request['post_type']) )
      $request['post_type'] = 'any';
      return $request;
      }
      add_filter('request', 'post_type_tags_fix');

      I created a trac ticket for this bug and Scribu sent the code.

      Here’s the ticket: http://core.trac.wordpress.org/ticket/14589

      Reply
  10. Michael
    Michael Published |

    I’d much rather have a term meta table than your first two proposed solutions.

    Instead of manually creating an actor post page and hiding the auto-generated actor taxonomy page, the ideal solution for me would be to have more robust control over the taxonomy page and use only that. A term meta table would help with that a lot (and would also let people who are so inclined link it to a custom post type — there’d be a lot of flexibility).

    Either way, given how important custom taxonomies are becoming, just having that extra “description” field isn’t cutting it.

    Reply
  11. Jonathan
    Jonathan Published |

    I have been a regular user of Wordpress for quite a while now and this was always very taxing for me to understand. You may not have provided the perfect answer but I can assure you it has cleared up a lot of my confusion

    Reply
  12. Victor Teixeira
    Victor Teixeira Published |

    Hi Justin, thanks for the great tutorial.

    I think the best solution is creating post type relationships without taxonomies at all.

    Wordpress should really add this to core.

    Maybe with a relations table or just a hidden taxonomy, like Scribus plugin.

    There’s also another plugin called Relation post types that creates relations through a relations table.

    Another good thing would be to relate taxonomies.
    So we could have a Country taxonomy and a City taxonomy. And each city could have a country as parent.
    I really think this is trivial stuff when building a custom app without a cms. Really basic relationship going on.
    I don’t know why all CMSs makes this kind of stuff so difficult to accomplish.

    Reply
    1. Mike Schinkel
      Mike Schinkel Published |

      Hi @Victor Teixeira:
      “I think the best solution is creating post type relationships without taxonomies at all. WordPress should really add this to core.”

      Ironically 3 days after you made that comment I just so happen to have proposed the exact same thing on trac:

      http://core.trac.wordpress.org/ticket/14513

      Unfortunately I was told that the need for relating posts was “an unimportant edge case” (I’m paraphrasing.) Feel free to add you comments on the ticket if you agree that with that it is not an edge case and that it is indeed important.

      -Mike

      Reply
  13. The WordPress Web Warlock
    The WordPress Web Warlock Published |

    Nice tute, Justin —quite the state-of-the-question.

    I went the termmeta way. It’s one of the features of a termmeta-based plugin I’m developing (expected the RC somewhere September-October). I call this feature the “description posts”, for they were originally intended as some kind of extended term description. I wanted a “rich description” without having to remove the kses filter ;) .

    I explained the process in wp-hackers: http://lists.automattic.com/pipermail/wp-hackers/2010-August/033729.html . This info is a bit outdated now.

    In a nutshell, there is postmeta for the posts to point to the term, there is termmeta for the term to point to the post. All termmeta is loaded and cached at init, wp_options like.

    You can see it in action in my website —all the http://web-warlocks.net/wordpress/descriptions category and its children cats are made of these “description posts”.

    I think that termmeta is the way to go. It can work with regular post types and core taxonomies, out of the box. (I use only posts and categories. I’ve got a nice term meta replacement for taxonomies, called “term groups”.) And it can work nicely with other termmeta: for instance, I’m using termmeta in those sections of my website to mark which Page they link to (nicest Permalinks ever, mixing terms with the “untampered potential of WordPress” :) ), which template to use (when needed), what function to use to format the posts, &c.

    I’ve seen some four or five termmeta-thingie-plugins (scribu listed four in wp-hackers, add mine and “Custom Fields for Everything”), not checked all, though. I’m disappointed with the ones I’ve seen: they mostly add just the basic API, not that hard to do once you’ve dealt with dbDelta (oh, a camelCase here??). Some don’t bother to use cache at all (!!!). And all, AFAIK, treat term meta as if were the “regular meta” (postmeta, usermeta…), which IMHO is a big mistake: termmeta works better when used as wp_options. An autoload column could have some use for the rarer termmeta, but treating termmeta as postmeta is a big performance drawback. Too much queries, too much time lost. For that 300+ terms website, the termmeta is less than half the size of the options table :)

    -Xavier

    Reply
  14. mitra kerja
    mitra kerja Published |

    Several of the ideas associated with this write-up happen to be very good but had me wanting to know, did they really mean that? One thing I have got to mention is your authoring expertise are very very good and I will be returning back again for any fresh post you make, you may possibly have a brand new enthusiast. I bookmarked your main web page for personal reference.

    Reply
  15. Matt
    Matt Published |

    I have an issue.. when installing multisite all my custom taxonomies do not show up… do you know what could cause this?

    Reply
  16. Isaac Foxx
    Isaac Foxx Published |

    I wish we didnt have to add so much code to our functions.php to get this working. but ultimately I think this works good enough for me.

    Any clue how to create a list in a sidebar that would only show the posts(custom-post-type) belonging to the CURRENT custom taxonomy term? (asssuming only one term is added at a time to each post.

    Ive been able to retrieve the current taxonomy term, but had problems with my template in the sidebar.

    Reply
  17. Mike Schinkel
    Mike Schinkel Published |

    Hi @Justin:

    Interesting article.

    Looking at what you have above it seems that choosing “actors”, “producers”, “writers” and “directors” as separate taxonomies is a “hammer-nail” approach; i.e. you only had taxonomies in WordPress to help you so that’s what you used to solve the problem.

    What would make more sense to me (assuming we had the required support built into WordPress) would be to have a single taxonomy called “Roles” which would have terms for each of the different roles a person could have, and those terms when applied to a movie could relate to a Person post. Of course that may be where taxonomy meta comes in which I do agree would have great value in many contexts.

    And having a roles taxonomy would also support an infinite number of roles — have you seen how many roles a given movie can have? Some even have things like “2nd Unit Dog Washer” (okay, I made that up) but here’s an example, just look how many roles this one has: http://www.imdb.com/title/tt0111161/fullcredits#cast (I wouldn’t want to add a taxonomy for each of those!)

    But let’s turn this problem around and imagine if we had a “post relationship” table that could relate two posts of arbitrary posts types along with an optional relation to a taxonomy term. If we had that then we could envision a two column metabox in the admin for the movie add/edit page that allows the user to relate one or more people to a movie and associate a term from the role taxonomy (i.e.
    “actors”, “producers”, “writers”, “directors” and more) for each of those people as they relate to the movie. This would be less complex than using taxonomy meta and be more performant and require less storage space than using a longtext meta_value to form relations with a bigint post->ID fields.

    What we need to get us out of “hammer-nail” territory is a posts relationship table in core, hence this ticket to which Westi said “We are not trying to solve 100% of use cases only the more significant ~80%” implying that posts relationship are only useful for less than 20% of use-cases, and Jane said “Fairly niche use, etc. …I can’t see this as a real candidate for addition to core, given that the bulk of users would probably never need this.”:

    http://core.trac.wordpress.org/ticket/14513

    I’m thinking with all your experience you might actually be able to appreciate how wide-spread such a need for post relationships is and be able to add your respected opinion to the ticket in support of adding to core (something) that handles post relationship in a reasonable way.

    Thanks in advance for considering.

    -Mike

    Reply
  18. kimmy
    kimmy Published |

    Thank you for that comprehensive easy to follow post.. now i have a supplemental aid to teach my poor husband what is it all about taxonomy and stuff..

    Reply
  19. Erwin Odendaal
    Erwin Odendaal Published |

    Interesting article and a good starting point for me to start my own plants database. But you cannot search on the taxonomies. Viewing all movies by for example “Miramax” is not possible in your database. Let alone searching or selecting films by multiple terms like “Miramax”, “Quentin Tarantino” and “Uma Thurman”. And that’s something you really want to do when using a database.

    Reply
    1. Matt Stigall
      Matt Stigall Published |

      Therein lies the rub. Wordpress is getting closer and closer to being a true CMS/Database portal. There are some plugins available for you to add some search functionality, but the search capabilities are lacking in the core.

      Reply
  20. Tutorial Roundup: WordPress Custom Post Types and Automatic Galleries — Creativity Included
  21. Jonathan Wold
    Jonathan Wold Published |

    I’m planning out an implementation along similar lines where an audio/video presentation has a speaker (or speakers) associated with it and each term in the speaker taxonomy is a link to a custom post type (“Speakers”) where you can read the complete bio and see an index of presentations associated with a given speaker.

    My first thought was that there may be some unnecessary redundancy for the client by have a taxonomy named “Speakers” under the presentation post type along with a custom post type of “Speakers. After further thought, I realized there may be a deeper issue.

    On the presentation page I want to include a list of the speaker(s) associated with a given presentation, displaying their thumbnail and a short description (a step beyond merely referencing the term). Now, that “speaker” is a part of the post’s taxonomy, but the thumbnail and bio should be drawn from the custom post type. Otherwise, I have to figure out how to associate a thumbnail with the taxonomy term and then deal with a separate description of the speaker in the term itself versus what I would pull from the custom post type.

    Does my case make sense? If so, any thoughts on how you’d pull this off?

    As best as I can resolve, I need to associate the taxonomy term with the custom post type and, ideally, I’d like to do it at a level that didn’t require redundant steps (such as adding a speaker twice) from those managing the site. Once associated, I can query the post type for the details I need (thumbnail, short description, etc).

    Reply
  22. jason peter
    jason peter Published |

    waooo~it’s complex! Only guys with related knowledge can really handle, anyway, thank you!

    Reply
  23. Version 0.4 | scribu
    Version 0.4 | scribu at |
  24. Richard
    Richard Published |

    I’ve posted a partial solution on the WP forums here:

    http://wordpress.org/support/topic/partial-solution-to-showing-single-post-for-a-term?replies=1

    It uses get_term_by to extract a value and pass it to WP_Query.

    The advantages are that every term can be displayed in a unique way e.g. Term A can go to a single post, Term B to a single image, Term C to multiple posts, Term D to multiple images etc.

    The disadvantage is that it doesn’t seem possible to display comments on the single post.

    Reply
  25. Nikole Gipps
    Nikole Gipps Published |

    I’ve been experimenting with this concept to see if I can fix the problem of previous/next posts not working for custom posts. For example, if I make a custom post “portfolio” and then have a single-portfolio.php template, I can’t use the previous_post_link functions to scroll through them. Any ideas on this?

    Reply
  26. Ezhil
    Ezhil Published |

    Hi justin,
    I have been trying to implement colors and images for different taxonomy,just like in http://mashable.com/#site_footer .i dont know what parameter to set for it could you please help me out.

    ID, 'sports', '', ',', ''); ?>

    iam able to show the taxonomy but i need to set color for each taxonomy.In my case their are different sports,how to set different color foe each sports.

    Reply
  27. steroizi
    steroizi Published |

    nice concept … i have to research something now …

    Reply
  28. elba
    elba Published |

    Thank you for this post and for the others that help us understand post types, taxonomies and terms.

    I have a couple of questions that wouldn’t let my mind rest:

    The actor post doesn’t lists the productions he participated in.

    What if the actor is also the director of the movie?
    Both of his names (under the roles) on the movie post would link to the same person post?

    More of the problem is that how can one show not just name (term) under the right role (taxonomy) but the character he is playing?

    And more: what if a character has multiple actors?

    Is there a way to show custom posts much like it was a taxonomy with terms on an other custom type post edit page? Probably not.

    I can’t figure it out.

    Reply
  29. Bonjour Tristesse
    Bonjour Tristesse Published |

    Thanks Justin,

    I’m usually afraid of code but I was still able to follow your methods easily.

    I have it implemented in a test blog now and just need a couple more tweaks to make it live.

    Reply
  30. Jack
    Jack Published |

    This can work well with our products as we have different types, brands, models, etc.

    However, I am having nosebleeds seeing all that coding & stuff. I hope you get to create a plugin for noobs like me.

    Reply
  31. James
    James Published |

    Yup same here, I’ve also been using scribu’s plugin for a while now and it works great for me.

    Took me a while to get my head around the code but all sweet now.

    Reply
  32. Phil
    Phil Published |

    Hi Justin,

    First of all, thanks for all your great tutorials. You have no idea how helpful you’ve been to me!

    I’m just moving my site to a new server and am planning on making better use of the custom post types function. One thing, I can’t seem to get to work is to use the same categories and tags across different post types.

    For example, I have articles that all refer to the broader topic of, let’s say, iPods. The different articles may have different types, though (How-To, Tip, Hack, Review, etc.). I would like to segregate the various types by using the post_type function, but – when browsing the category “iPod” – all of the different post types should show up in the loop.

    Right now, following your tutorials, I’ve added

    ‘taxonomies’ => array( ‘post_tag’, ‘category ‘)

    to the code, which works fine on the edit page. I can clearly see all existing tags and categories and can assign them to the custom posts. However, what happens then is that wordpress changes the permalink for those new categories and adds a parallel category (tag) to my site for each additional post type. Even though, it’s the same name when I write it.

    Is there any way around this?

    Thanks for your help!

    Reply
  33. Peter J.
    Peter J. Published |

    This is minor, but may avoid warnings for cut-and-pasters: the semicolon at the end of the elseif shouldn’t be there.

    elseif ( !empty( $post_id ) );
    		return $post_id;

    As written you’ll never get to the return false;, since the elseif is actually empty.

    Reply
  34. Manny Fresh
    Manny Fresh Published |

    Shibashake has a nice tutorial of how to implement metadata onto Taxonomy terms. Maybe you can use something like this to store the post id with the term its associated with?

    Reply
    1. Manny Fresh
      Manny Fresh Published |
      Reply
  35. Shane
    Shane Published |

    Hi

    Great tutorials.
    I’ve created several content types but I’m come across a problem. I have a hierarchal custom taxonomy which I called “country” and then using this assign my posts related to whatever country name they are associated with.

    In my case I would be using the same taxonomy for multiple content types. Now when I call up an archive page or single for a custom post type and use e.g. get_the_term_list it will show the country name, but when clicking on it, it shows posts of other content types as well (that have the associated taxonomy).

    How do I limit the taxonomy to that particular content type for users when browsing but still have the flexibility to use it across content types when posting content?

    Reply
    1. todd
      todd Published |

      @Shane – Have you found an answer to this yet? I am stuck on basically the same question. I want to display the terms of a taxonomy, but limited by a custom post type.

      I have two Custom Post Types: “Rentals” and “Sale”. On the main Rentals page (a custom template file for a regular wp page), I am using wp_list_categories() to list the Terms for my “Brands” Taxonomy. But I want it to show ONLY the Brands that are associated to Rentals post-types.

      Reply
  36. Darren
    Darren Published |

    Thanks so much for this! Very helpful.

    I’m trying to implement this on a site that’s actually similar to the idea of a movie database. What I’m wondering is if it’s possible to apply images (headshots) to specific taxonomy terms, so that each “person” and each “actor” shows up with their photo next to their name. Is there any way to do that?

    Reply
  37. Shovan
    Shovan Published |

    Amazing post, I was using custom field and it was a pain. Thanks for your solution will give it a go

    Cheers,
    Shovan S

    Reply
  38. eDMAR
    eDMAR Published |

    I have a custom taxonomy called actor / actress, I created a type called a post BioAtor, I wonder if you like that when redirecting to already have a post type with the same name, Brad Pitt example, when you click to be redirected to this person post type, and if no, go to the archives taxonomy.

    I found one more code was not like he would have redirected all, and if there was no post type, he sent the index to the blog.

    Thank you in advance for your help.

    Reply
  39. Jacob
    Jacob Published |

    Is this still the best way to do this if using 3.2?

    Reply
  40. Sandra
    Sandra Published |

    Nice Post.
    Thanks for adding code to create the movie and person post types.

    Reply
  41. Matthieu Smael
    Matthieu Smael Published |

    Hello,

    I really like your tutorials, my only problem is how to implement them … i’m pretty new to all this stuff…

    For exemple if I use your option #1 :
    – WHERE and HOW do I call them ?

    Thanks !

    Reply
  42. Custom Post Types Are Not Posts | Ipstenu on Tech
  43. The structural weaknesses of WordPress | shawfactor
  44. Greg Turner
    Greg Turner Published |

    What am I missing? I used the code above for registering the custom post type ‘movie’ and registering the taxonomy ‘actor’ that relates to ‘movie. The screen for managing ‘actor’ works as expected. But when I go to add or edit a ‘movie’, I would expect to see the list of ‘actor’ there to choose from, but no such list appears. Although there is a heading on the right of the edit page that says ‘Actors’. What am I missing? Thanks

    Reply
  45. Tim
    Tim Published |

    Hallo,

    first thx for the cool tutorial about taxonomie term linking… +2 :)

    i have one question about this tutorial i am trying add more as one taxonomy to person posttype.

    Movie (post type)
    Person (post type)
    Actor (taxonomy)
    Director(taxonomy)

    add_filter( 'term_link', 'my_term_to_type', 10, 3 );
    
    function my_term_to_type( $link, $term, $taxonomy ) {
    
    	if ( 'actor','director' == $taxonomy ) {
    		$post_id = my_get_post_id_by_slug( $term->slug, 'person' );
    
    		if ( !empty( $post_id ) )
    			return get_permalink( $post_id );
    	}
    
    	return $link;
    }
    
    function my_get_post_id_by_slug( $slug, $post_type ) {
    	global $wpdb;
    
    	$slug = rawurlencode( urldecode( $slug ) );
    	$slug = sanitize_title( basename( $slug ) );
    
    	$post_id = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_name = %s AND post_type = %s", $slug, $post_type ) );
    
    	if ( is_array( $post_id ) )
    		return $post_id[0];
    	elseif ( !empty( $post_id ) );
    		return $post_id;
    
    	return false;
    }

    but its not helpe mee can u help mee how i can use it for multi taxonomies……

    Reply
  46. Wilfrid
    Wilfrid Published |

    Hello Justin,
    Thanks for this awesome tips.
    I wake up this article because I have a query.
    if taxonomy exists, but not the article it returns a 404 page.
    Would it be possible to refer to a specific page 404 or the original taxonomy archive ?

    Sorry for my google translate english

    Reply
  47. WPSmith | Helpful Links
    WPSmith | Helpful Links at |
  48. amit
    amit Published |

    Very Helpful. what if a have a common taxonomy term for 3 different taxonomy.
    for ex….

    mywebsite..com/hotels/delhi
    mywebsite.com/tours/delhi
    mywebsite.com/delhi (<– Page)

    is it a bad url practice?

    Reply
  49. Building an Admin System in WordPress With Custom Post Types, Taxonomies and Meta Boxes - White Heat Design
  50. How WordPress Saves Lives - Paul Clark
  51. Casey Patrick Driscoll | Post to post relationship recap

Leave a Reply

By submitting a comment here you grant this site a perpetual license to reproduce your words and name/Web site in attribution.

Please use your real name or a pseudonym (i.e., pen name, alias, nom de plume) when commenting. If you add your site name, company name, or something completely random, I'll likely change it to whatever I want.

css.php