Execute function when post is published

The correct action is 'draft_to_publish'.

To be sure you used the correct status try to get a list of all registered post statuses (including custom statuses) with:

<pre><?php print '- ' . implode( "\n- ", array_keys( get_post_stati() ) );?></pre>

On a vanilla installation you should get:

  • publish
  • future
  • draft
  • pending
  • private
  • trash
  • auto-draft
  • inherit

Note that publish_post is called each time you edit a published post.

Note also get_post_stati() is one of these unpredictable names in WordPress: it is plain wrong. The plural of the noun status is statuses in English and statūs in Latin. 😀

You could also hook into 'transition_post_status', depending on your needs. You get the new and the old status as arguments, the third argument is the post object. It will catch future_to_publish too, and also posts that were trashed once and republished now (trash_to_publish).

Example:

add_action( 'transition_post_status', 'a_new_post', 10, 3 );

function a_new_post( $new_status, $old_status, $post )
{
    if ( 'publish' !== $new_status or 'publish' === $old_status )
        return;

    if ( 'post' !== $post->post_type )
        return; // restrict the filter to a specific post type

    // do something awesome
}

Leave a Comment