我为使用此功能的用户设置了一些针对某些评论的自定义元:add_comment_meta( $wp_comment_id, 'accepted', true );
我想要做的是在他们的个人资料/作者/用户名上为每个用户显示他们有多少这些特殊评论,例如,如果一个用户发表了 20 条评论并且 5 有这个接受等于 true 的元数据,那么值将是5.
我怎么能这样做?谢谢。
我假设这是最新版本的 wordpress。您可以在此处查看数据库架构图:http: //codex.wordpress.org/images/9/9e/WP3.0-ERD.png
我还没有测试过这个,但是像这样的东西应该可以解决问题:
<?php
getCommentCount('John', 'accepted', 'true');
function getCommentCount($author_name, $meta_key, $meta_value){
if(empty($author_name)){
return;
}
$author_name = trim($author_name);
$sql = 'SELECT count(*) FROM ' . $wpdb->comments . ' comments '
. ' INNER JOIN ' . $wpdb->commentmeta . ' meta ON comments.comment_ID = meta.comment_id '
. ' WHERE comments.comment_author = %s AND meta.meta_key = %s ANd meta.value = %s ';
$commentCount = $wpdb->get_var($wpdb->prepare($sql, $author_name, $meta_key, $meta_value));
return $commentCount;
}
我不明白“计算元数据”是什么意思...元数据中的哪个
值..您是指用户的元数据还是评论。
无论如何 - 为了计算特定用户的评论,您可以...
将此代码放入您的函数 php
function countUserComments($userID) {
$args = array(
'user_id' => $userID
);
$comments = get_comments( $args );
echo count($comments);
}
您可以在任何有可用用户 ID 的地方使用它,就像这样
<?php echo countUserComments($user->ID); ?>
.
希望这对任何人都有帮助;)干杯,圣人