I’m writing a plugin that fetches some data from an XML file and then creates a post based on its content.

I was thinking of using wp_insert_post() to publish the new post, but I have some functions that are hooked to publish_post.

After digging in the \wp-includes\post.php, I couldn’t find any publish_post action hooks that are triggered by this event.

There were only these hooks related to my case:

// Fires once an existing post has been updated.
do_action( 'post_updated', $post_ID, $post_after, $post_before);
// Fires once a post has been saved.
do_action( 'save_post', $post_ID, $post, $update );
//Fires once a post has been saved.
do_action( 'wp_insert_post', $post_ID, $post, $update );

Am I missing something? Or doesn’t wp_insert_post() trigger the publish_post action?

1 Answer
1

It’s triggered in wp_publish_post() that calls:

wp_transition_post_status( 'publish', $old_status, $post );

that fires up action calls, dynamically with:

do_action( "{$new_status}_{$post->post_type}", $post->ID, $post );

where "{$new_status}_{$post->post_type}" becomes "publish_post" in your case.

Leave a Reply

Your email address will not be published. Required fields are marked *