publish_post: How to check if WP post is newly Published or Edited?

October 14, 2010
By @NetCrunched

Recently I had some issues with publish_post WordPress action hook. In my Twitter Goodies plugin I want to publish tweet to my twitter account ONLY when I publish new post but NOT every time when I edit it.

wordpress hooks 300x180 publish post: How to check if WP post is newly Published or Edited?

I figure out the solution for above problem and wanted to share with you guys.

1) Add publish_post action hook to your plugin

add_action('publish_post','your_function_name');
add_action('publish_page','your_function_name');

2) In your function add below if conditions to distinguish newly published post or edited post.
For Newly Published Post:

if (($get_post_info->post_status == 'publish' || $_POST['publish'] == 'Publish') && ($_POST['prev_status'] == 'draft' || $_POST['original_post_status'] == 'draft' || $_POST['original_post_status'] == 'auto-draft' || $_POST['prev_status'] == 'pending' || $_POST['original_post_status'] == 'pending' ) )

For Edited Post:

if ((( $_POST['originalaction'] == "editpost" ) && ( ( $_POST['prev_status'] == 'publish' ) || ($_POST['original_post_status'] == 'publish') ) ) && $get_post_info->post_status == 'publish')

Where, $get_post_info = get_post($post_id);

I hope above will help you. If you have any better solution or other way then please update here in comment section. It will help all of us in plugin development.

Tags: , , , , ,



Links

Links