My plugin does stuff whenever a post is published and comments are enabled for that post. It works fine when publishing a post as normal, but if a scheduled post is published, it doesn't.
Here's what my code looks like:
function do_stuff( $new_status, $old_status, $post ) {
global $post;
if ( !comments_open( $post->ID ) ) {
return;
}
/* do stuff */
}
add_action( 'transition_post_status', 'do_stuff', 10, 3 );
After some debugging, I was surprised to find the problem was comments_open()
returning FALSE
, every single time a scheduled post was published, regardless of the "Allow Comments" setting.
Why is this happening?
$post->comment_status
returns in these cases?$post->comment_status
is empty when a scheduled post is published. I'm guessing this is whycomments_open()
doesn't work, because the$post
object hasn't been fully initialized yet...