[wp-hackers] Best Practice for hooking wp_insert_post()

Mike Schinkel mikeschinkel at newclarity.net
Sun Jul 25 15:49:58 UTC 2010


On Jul 24, 2010, at 4:54 PM, Andrew Nacin wrote:
>>> I'd like to ask about best practices for hooking wp_insert_post().
>>> The questions are when and why to use "save_post" vs.
>>> "wp_insert_post" and more importantly what's the best way to
>>> determine if a post is being added or updated, or is an auto-post?
>>> Actually, I'd love to have that all clarified.
> 
> Auto-drafts are new in 3.0. They're designed to prevent the need to wait for
> an autosave, which is what you're describing. The moment you visit the
> post-new.php screen, you now have a post ID in the form of an auto-draft.
> This allows you to immediately attach media, work with custom fields,
> featured images, and the like. The auto-drafts that don't turn into posts
> are then cleaned up after a week or so.
> 
> Here's the ticket we managed the development of this in:
> http://core.trac.wordpress.org/ticket/11889.

Thanks. That really helps.  

However, it didn't answer the question of when to use the hooks "save_post" instead of "wp_insert_post";  Is there a reason to have two; should we do stuff in one and not the other, or is the fact we have two just legacy cleanup?

> It does have the side effect of triggering save_post. The post status
> transition hooks are probably better to use in most cases however.

Ah, I didn't notice those since they were called in a function below wp_insert_post() and I didn't think to drill down. Thanks!

On Jul 25, 2010, at 12:18 AM, Kunal Bhalla wrote:
> I had to do that recently for my project -- create an 'activity' post when a
> post of a particular type was published. As far as I remember,
> wp_insert_post is called for everything, updating, whatever. I found it
> simpler to hook into the transition post status (like nacin said), checking
> that the post _was_ of the type I needed (automatically eliminating drafts,
> auto-saves), whether the new status was publish, and that the old status
> wasn't.
> 
> I've paste-binned the relevant bits of code here:
> http://pastebin.com/z9SndiU5

Thanks Kunal for including a link to the code.

On Jul 25, 2010, at 2:06 AM, Otto wrote:
> I've had to do this a few times. The only way I found that doesn't
> suck is to hook to transition_post_status like this:
> 
> add_action('transition_post_status','whatever',10,3);
> 
> Your whatever function will get $new, $old, $post, such that
> $new = new post status ('publish')
> $old = old post status
> $post = the complete post object
> 
> By comparing new and old, you can determine if a post is being updated
> or published or what.
> 
> Every other way I tried ended up with one-off anomalies.

Thanks.  This was exactly the kind of answer I was looking for.

Any thoughts though on what "artifacts" to look for when using this (for adding a new one, and just in general)?  Should I compare statuses, should I look at IDs, guids, post_titles, post_names, other?  Any explicit insight will be helpful.

-Mike




More information about the wp-hackers mailing list