The publish_post hook

The publish_post hook is one I’ve seen a lot in tutorials and such. Even has its own page in the codex.

But surprise! That hook doesn’t actually exist, at least not if you search for do_action\(\s?.publish_post.

When I was learning to dig into core, this baffled me. What was this publish_post sorcery? Why wouldn’t it work on my custom post types???

Here’s a secret: The actual hook in core looks like this "{$new_status}_{$post->post_type}". It’s part of a small function in wp-includes/post.php (line 3321 at the time of writing), and there’s another equally useful variable hook right before it.

function wp_transition_post_status($new_status, $old_status, $post) {
	do_action('transition_post_status', $new_status, $old_status, $post);
	do_action("{$old_status}_to_{$new_status}", $post);
	do_action("{$new_status}_{$post->post_type}", $post->ID, $post);
}

There are quite a few of these variable hooks in core, I encourage you to go look at them, here’s a list I scraped together:

Continue reading The publish_post hook