[wp-hackers] How to create relations between custom post types?

Michael Pretty mpretty at voceconnect.com
Sat May 29 20:41:40 UTC 2010

I've had to do a similar thing to this and start by saying, its not the 
easiest to do.

Like how Mike mentioned below, you setup both the taxonomy and the 
post_type, but turn off the rewrite rules for the taxonomy.

-For the taxonomy, give it a different capability strings so that normal 
users can't add/delete them directly.
-Use the save_post and delete_post actions to automatically add and 
delete the given term for the custom post
-Link the custom post to its term by storing the term_id in the 
post_meta of the post.  We've learned that trying to match them up via 
slug/post_name fields becomes a nightmare due to conflicts.
-Like Mike mentioned below, use the post_type's permalink to pull in the 
single-{post_type}.php and display the data from the custom post.   But 
then have a query_posts call within that page that pulls in the child 
posts based on the term set in the post's meta field.

Sorry if this isn't a clear explanation.  If you have specific 
questions, I can explain further.


On 5/29/10 12:42 PM, Mike Schinkel wrote:
> On May 29, 2010, at 2:49 AM, Edward de Leau wrote:
>> would it be possible to support cases where a custom post type and a
>> custom taxonomy type carry
>> the same name? I think they do not conflict.
>> (It enables creating relations between custom post types).
> Yes, I'm working on a project that will do exactly that.  I believe that will become a common design pattern now that we have custom post types.
> You will need, however, need to disable rewrite when you register your taxonomy by setting it to *false* like so:
> 	register_taxonomy('mysterybox', 'building', array(
> 		'hierarchical'    =>  true,
> 		'label'           =>  __('Mystery Boxes'),
> 		'singular_label'  =>  __('Mystery Box'),
> 		'rewrite'         =>  false,
> 		)
> 	);
> You will also need to develop your single-mysterybox.php such that it loads and displays your content from your applicable mysterybox and its related buildings. You comfortable with handling that part now that you understand how to resolve the URL routing issue, right?
> -Mike
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers

More information about the wp-hackers mailing list