Write a function that retrieves the result you want in your plugin.
function retrieve_custom_rating( $post_id ) {
// use $post_id to look up your custom rating
return $rating;
}
Then, that function will be available in your template files.
Thanks for the replay Andrew, I am working a plugin I have created. In a comment addition plugin, I have added the ability to add a rating (1-5). I am now calculating and trying to display the average of the ratings $avg_rating = ($tot_stars / $no_of_comments);. I have logic that allows this to work and be displayed within the comments themselves, however I want them to be displayed on my custom post template. I have tried adding my calculations to a function as you have recommended as such:
function calc_avg( $post_id ) {
// Calculate rating averages
$args = array(
'ID' => $post_id,
'status' => 'approve',
);
$comments = get_comments( $args );
foreach( $comments as $comment ) {
$tot_stars += get_comment_meta( $comment->comment_ID, 'rating', true );
}
$no_of_comments = get_comments_number( $post_id );
$GLOBALS['$avg_rating'] = ($tot_stars / $no_of_comments);
if ($no_of_comments > 0) {
$avg_return = $avg_return;
return $avg_return;
} else {
return $avg_return;
}
}
however echo $avg_return does nothing. Nor does echo calc_avg($post_id) and using GLOBALS or global has not helped either. Any other ideas?
Are you sure that $post_id is being set properly? Have you done a
echo $post_id;
die();
As the first two lines of the function to verify that you are passing something?
If you’re in the loop you could pass get_the_ID() too.
while( have_posts() ) {
the_post();
echo calc_avg( get_the_id() );
}
First thing is to verify that you are indeed passing the correct id to the function. If the function wasn’t included in the template file, you would have received a fatal error, so we’re on the right track.
So I have tried passing echo $post_id; and `while( have_posts() ) {
the_post();
echo calc_avg( get_the_id() );
}`
Both did not pass any value, however there were no fatal errors. My loop is slightly adjusted, maybe this could affect it?
<?php query_posts(array('post_type'=>'products')); ?>
<?php $mypost = array( 'post_type' => 'products' );
$loop = new WP_Query( $mypost ); ?>
<?php wp_reset_query(); ?>
EDIT: echo the_ID(); returns an integer
Can you put the whole template in a pastebin or gist please?
Sure,
The template:
http://pastebin.com/82eQw7pT
and the function as it is currently in my comment plugin:
function calc_avg( $post_id ) {
// Calculate rating averages
$args = array(
'ID' => $post_id,
'status' => 'approve',
);
$comments = get_comments( $args );
foreach( $comments as $comment ) {
$tot_stars += get_comment_meta( $comment->comment_ID, 'rating', true );
}
global $testvar;
$testvar = 1;
$no_of_comments = get_comments_number( $post_id );
$GLOBALS['$avg_rating'] = ($tot_stars / $no_of_comments);
global $avg_return;
if ($no_of_comments > 0) {
$avg_return = $avg_return;
return $avg_return;
} else {
return $avg_return;
}
}