[wp-hackers] Mirroring a custom_post to a custom_taxonomy for post2post relation [was: Time for a wp_post_relationships table?]

Otto otto at ottodestruct.com
Tue Aug 3 21:03:29 UTC 2010

On Tue, Aug 3, 2010 at 2:58 PM, Lox <lox.dev at knc.nc> wrote:
> Otto,
> Taking the following case:
> - two custom post types (movies and actors) and a custom taxonomy actors.
> - the actors taxonomy should contains all actors created through the
> actors custom post type admin ui, not more, not less.
> - then, actors term can be applied to any movie. This gives the
> ability to retrieve all movies where an actor plays in, or, all actors
> of a specific movie.

I'd also create another "mirrored" taxonomy for movies too, to do it
in reverse.

> 2010/8/4 Otto <otto at ottodestruct.com>:
> Taking an actor creation. When the actor get created (save_post hook)
> I apply a term in actor taxonomy with the actor post title as the
> term, using wp_set_object_terms ?

I'd use the transition_post_status hook instead, but basically, yes.

> When it is a post update, I need to update the taxonomy using wp_update_term ?

Yes, if the information in the post changes enough to warrant it.
Remember that posts are tied to terms only by ID. When you update a
term, that update applies to everything referring to that term. So if
the term is the Actor's name, then you'd take the old name and change

> So basically, I add an action 'save_post' where I check:
> - if post type is actor
> - if it is a post update or a post creation
> - act accordingly
> Is that it?

Yes, but again, transition_post_status might be a better place. You
could do it with save_post too, I guess. Haven't looked closely at it.

> Not automatically: if an actor get deleted, the related term in actors
> taxonomy could also have been applied to one or more movies, and thus
> won't be deleted

True, but you might not want to delete the term entirely out of all
posts. Perhaps you're going to recreate the actor's post and still
want the term to exist, so it can still be tied to all his movies.

> transistion_post_status ? Is that an action hook? If so it is undocumented...

Look at the wp_transition_post_status() function. This is called
whenever a post is inserted or updated or published or whatever. It's
very handy.


More information about the wp-hackers mailing list