我正在使用以下函数/简码来使用 ACF 输出平均全球评级:
function get_average_rating($post_id) {
$rating_sum = 0;
$reviews_of_post = get_posts( array(
'post_type' => 'avis',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'produit',
'value' => $post_id,
'compare' => '=',
),
),
) );
if ( empty( $reviews_of_post ) ) {
return 0;
}
foreach ( $reviews_of_post as $review ) {
$rating_sum += get_field( 'note_client', 'post_' . $review->ID);
}
return number_format((float)($rating_sum / count( $reviews_of_post )),1, ',', '');
}
add_shortcode( 'note-clients', 'get_average_rating');
它总是返回 0,除非我手动输入帖子 ID,例如:
'meta_query' => array(
array(
'key' => 'produit',
'value' => 1234,
'compare' => '=',
),
),
我怎样才能解决这个问题 ?
非常感谢 !