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

Otto otto at ottodestruct.com
Sun Jul 25 06:06:38 UTC 2010


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.

-Otto



On Sat, Jul 24, 2010 at 9:14 AM, Mike Schinkel
<mikeschinkel at newclarity.net> wrote:
> Hi all:
>
> 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.
>
> I came up with the following "artifacts" that imply to me it is in "insert" mode but I can't be sure:
>
> $post->post_status=='auto-draft'
> empty($post->guid)
> empty($post->post_name)
>
> Thanks,
>
> -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