I want check, if user commented post, in comment_post action. So i make this:
function checkUserComment( $comment_ID ) {
$comment = get_comment( $comment_ID );
$postID = $comment->comment_post_ID;
$authorID = $comment->user_id;
// If user commented this post already, don't update post meta.
if( get_comments( array( 'post_id' => $postID, 'user_id' => $authorID ) ) )
return;
update_post_meta($postID, 'testMeta', 'testValue');
}
add_action( 'comment_post', 'checkUserComment', 10, 2 );
It must doesn't update post_meta if user comments first time. But it updates post_meta anyway.
I want check, if user commented post, in comment_post action. So i make this:
function checkUserComment( $comment_ID ) {
$comment = get_comment( $comment_ID );
$postID = $comment->comment_post_ID;
$authorID = $comment->user_id;
// If user commented this post already, don't update post meta.
if( get_comments( array( 'post_id' => $postID, 'user_id' => $authorID ) ) )
return;
update_post_meta($postID, 'testMeta', 'testValue');
}
add_action( 'comment_post', 'checkUserComment', 10, 2 );
It must doesn't update post_meta if user comments first time. But it updates post_meta anyway.
From the WordPress Codex:
comment_post is an action triggered immediately after a comment is inserted into the database.
Instead, I think what you want to try is to hook into preprocess_comment and not comment_post.
is_array()
and!empty()
but same. I wonder, when action check it? After comment submisson or before? – wpdev Commented Dec 23, 2018 at 11:25