Tell us what you are trying to do?
I'm summarizing star ratings:
1. Average rating for a course
2. Total ratings
Is there any documentation that you are following?
Yes:
https://toolset.com/forums/topic/star-rating-with-averagetotal-user-comments-with-rating/
https://toolset.com/forums/topic/review-system-how-to-get-average-rating/
I've written a shortcode to return the average rating:
----------------------------------------
add_shortcode( 'average_rating', 'sbma_average_rating_shortcode' );
function sbma_average_rating_shortcode( $atts ) {
if ( ! class_exists( 'Types_Main' ) ) {
return '<p>The Toolset Types plugin is not active</p>';
}
if ( ! class_exists( 'WPV_View_Base' ) ) {
return '<p>The Toolset Views plugin is not active</p>';
}
$atts = shortcode_atts( array(
'course_id' => '',
), $atts );
$course = $atts['course_id'];
$args = array(
'post_type' => 'course-testimonials',
'meta_query' => array(
array(
'key' => 'course_id',
'value' => $course,
),
),
);
$reviews = new WP_Query( $args );
$total_rating = 0;
$count = 0;
if ( $reviews->have_posts() ) {
while ( $reviews->have_posts() ) {
$reviews->the_post();
$rating = get_post_meta( get_the_ID(), 'wpcf-testimonial-course-stars', true );
if ( $rating ) {
$total_rating += $rating;
$count++;
}
}
}
if ( $count > 0 ) {
$average_rating = round( $total_rating / $count, 1 );
return "<h4>" . $average_rating . " out of 5</h4>";
} else {
return "<h4>No reviews found</h4>";
}
}
----------------------------------------
1. There are a number of comments in the course-testimonials CPT that match the course ID submitted in the shortcode.
2. I've attached screen grabs of the Toolset setup FYI.
3. The query always returns 0 results.
4. I validated the query directly in SQL and there are no matches for the post_type = 'course-testimonials' with a custom field meta 'wpcf-testimonial-course-stars', so the code is functioning correctly.
----------------------------------------
SELECT
wp_posts.ID, post_type, post_name
FROM
wp_posts
INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id)
WHERE
wp_postmeta.meta_key = 'course_id'
AND wp_postmeta.meta_value = '909'
AND wp_posts.post_type = 'course-testimonials'
AND wp_posts.post_status = 'publish'
GROUP BY
wp_posts.ID
ORDER BY
wp_posts.post_date DESC
----------------------------------------
There is a logic problem I have. I'm assuming that there should be a direct relationship between the Toolset CPT 'course-testimonials' and the Toolset custom field 'course_id'. But there appears not to be. Please correct my assumptions.