繁体   English   中英

查找ACF自定义字段值之和以进行注释

[英]Finding sum of ACF custom field value for comments

我为评论创建了一些自定义字段,以允许在Wordpress网站上进行评论,该网站旨在将带有用户评论的酒店列为评论。 这些自定义字段之一是“ star_rating”字段。 我需要找到一种方法来获取所有值的总和,以便找到要在搜索结果中和列表资料中显示的每个帖子的平均“ star_rating”值。

我一直在尝试寻找star_rating字段值的总和。 我上次使用下面的代码,但是虽然看不到原因,但是它不起作用。 任何帮助将非常感激。

$ratings_sum = 0;
// Arguments for the query
$args = array();
// The comment query
$comments_query = new WP_Comment_Query;
$comments = $comments_query->query( $args );
// The comment loop
if ( !empty( $comments ) ) {
    foreach ( $comments as $comment ) {
        $ratings_sum+= $comment->star_rating;
    }
} else {
    // echo 'No comments found.';
}
echo $ratings_sum;

提前谢谢了。

如果您使用的是ACF自定义字段插件,请尝试使用get_field()函数。 您不能像这样访问自定义字段$comment->star_rating

尝试在以下自定义查询中获取星级:

SELECT SUM(meta_value) FROM wp_commentmeta WHERE comment_id IN (SELECT comment_ID FROM wp_comments WHERE comment_post_ID = '//your post id') and meta_key='star_rating';

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM