Skip Navigation

[Resolved] CPT Relationship to Custom Fields

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Our next available supporter will start replying to tickets in about 3.76 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 3 replies, has 2 voices.

Last updated by bradleyS 1 year, 9 months ago.

Assisted by: Luo Yang.

Author
Posts
#2548619
ScreenShotShot-2023-02-06 -10.36.35@2x.png
ScreenShotShot-2023-02-06 -10.36.59@2x.png
ScreenShotShot-2023-02-06 -10.27.57@2x.png
ScreenShotShot-2023-02-06 -10.27.39@2x.png
ScreenShotShot-2023-02-06 -10.26.48@2x.png

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.

#2549193

Hello,

Yes, you are right, the custom PHP codes you mentioned above is based on a custom field "course_id", and that solution is outdated.

I assume you are using below settings:
1) Two post types:
- course
- course-testimonials( with custom field "testimonial-course-stars")

2) In the single "course" post you can follow our document to query all related "course-testimonials" posts:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

Then use above "course-testimonials" posts to calculate the average value and total count value

#2549605
ScreenShotShot-2023-02-07 -11.07.30@2x.png

Thank you.

1. No. I'm only using the one Toolset CPT; course-testimonials (with a few custom fields including; 'testimonial-course-stars', 'testimonial-course-id'). The Toolset CPT is populated using a Gravity Forms form with 'Advanced Post Creation' Add-On. There is no Relationship with other CPT's. Thus the 'toolset_get_related_posts' is not relevant.

2. I refactored the code and it now works as expected. Posting the relevant snippet for others if it's helpful. The important, yet subtle point to note, is that when using WP for the query, one has to prepend wpcf- to the custom field slug. Note the meta-key value (lacking the wpcf-testimonial- → I also got the slug wrong) in the original function I posted.

----------------------------------------

$args = array(
'post_type' => 'course-testimonials',
'posts_per_page' => -1,
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'wpcf-testimonial-course-id',
'value' => $course_id,
'compare' => '=',
)
)
);

if ($permission == "Yes") {
$args = array_merge( $args, array(
'meta_key' => 'wpcf-testimonial-permission',
'meta_value' => 'Yes',
'meta_compare' => '='
));
}

$reviews = get_posts($args);
----------------------------------------

#2549615

My issue is resolved now. Thank you!