WordPress comment_form saving to wrong post

This was a doozy but it turned out to be a simple fix. I don’t yet know the cause but have seen a few rumblings of the W3 Total Cache plugin being the culprit.

In a custom them, you call comment_form to add the default WP comment form. You can do all sorts of changes but customizing is pretty difficult. It will automatically call comment_id_fields which adds the hidden fields for the parent comment (comment_parent; for comment replies) and post id (comment_post_ID; for the comment to attach it to).

I believe W3T is the problem because comments worked just fine before using it and the problem I found was the comment_post_ID defaulted to one number and never changed. Something was wonky but the fix was super simple.

comment_form takes two arguments: $args and $post_id. Yep…easy enough…just pass the second argument and you’re good to go for any post you want to add a comment to.

<?php comment_form( $args, get_the_ID() ); ?>

That’s it. get_the_ID refers to the current page/post ID, while in The Loop, so it’ll always be there. Just update your themes comments.php and enjoy the fix.

Hopefully this helps someone else as Google was sparse on results for this specific problem.