Logging Post Status Transitions in WordPress

Last week an interesting issue came up for a client. Somehow a few posts were moved from Published to Draft. Unfortunately, post status isn’t stored in revisions so it’s unlikely we’ll ever know who or how it happened. Luckily there’s a simple solution I found to log all post status transitions in post meta:

add_action( 'transition_post_status', 'log_post_transition', 10, 3 );
function log_post_transition( $new_status, $old_status, $post ) {
	if ( 'post' === $post->post_type ) {
		$current_user = wp_get_current_user();
		add_post_meta( $post->ID, '_post_status_log', sprintf( '%s %d, %s->%s', current_time( 'mysql' ), $current_user->ID, $old_status, $new_status ), false );
	}
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s