admin管理员组文章数量:1130349
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.
Share Improve this question edited Dec 23, 2018 at 12:31 wpdev asked Dec 23, 2018 at 11:05 wpdevwpdev 5492 gold badges13 silver badges28 bronze badges 4 |1 Answer
Reset to default 1From 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.
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.
Share Improve this question edited Dec 23, 2018 at 12:31 wpdev asked Dec 23, 2018 at 11:05 wpdevwpdev 5492 gold badges13 silver badges28 bronze badges 4- To narrow the problem you have to check each variable values, e.g., by echoing it. – Max Yudin Commented Dec 23, 2018 at 11:15
-
I checked all variables and print all them. Also tried
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 - And what is the problem exactly? You say it doesn’t work. How? – Krzysiek Dróżdż Commented Dec 23, 2018 at 11:59
- Updated question. – wpdev Commented Dec 23, 2018 at 12:32
1 Answer
Reset to default 1From 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.
本文标签: commentsHow to checkif user commented beforeon commentpost action
版权声明:本文标题:comments - How to check, if user commented before, on comment_post action? 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749072058a2311729.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


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