I am currently using nodecomments which replaces the default comments and makes comments actual nodes. The reason being I need to add a lot of custom fields to my comments including Fivestar. This is for a review site and I was not happy with the current state of drup's review modules, so I decided nodecomments would actually be the best.

Now it works and looks fine with Fivestar with one large expection. Say I create a page that has a photo on it and 10 user comments (reviews). Each users comment on that page had fivestar showing what that user rated the photo at, but I also want to show the overall tally or average vote on top of the page ( or anywhere for that matter).

I think this is possible using the votingapi but since every comment rating is a seperate node I might be up the creek?

Any ideas on how I can tally results from differant nodes ( even though they are a part of the same node)?


quicksketch’s picture

Yep! You're actually reproducing the exact situation fivestar was originally written for. Check out
http://www.goodcharlotte.com/music for an example.

The comments on the page are of 'review' type, which contain a fivestar rating widget (you'd need to register to post a review though).

The trick is setting the target Node ID in the fivestar field settings. Go to 'Manage fields' for your content type, click edit on your fivestar field. Then use the following PHP code in the Node ID field:

if (arg(0) == 'node' && arg(1) == 'add' && is_numeric(arg(3))) {
  return arg(3);

if (arg(0) == 'node' && is_numeric(arg(1)) && arg(2) == 'edit') {
  $node = node_load(arg(1));
  return $node->field_rating[0]['target'];

if (arg(0) == 'node' && is_numeric(arg(1)) && arg(2) != 'edit') {
  return arg(1);

drupal_set_message('An error occurred saving your review, no target was specified.', 'error');
return 0;

Note that you'll need to set 'field_rating' to whatever the name of your fivestar field is.

To display the combined rating on the target node, display the static theme function:

  $current_rating = votingapi_get_voting_result('node', $nid, 'percent', 'vote', 'average');
  print theme('fivestar_static', $current_rating->value);
quicksketch’s picture

Status: Active » Closed (fixed)
teamrob’s picture

Thats great thanks a lot, I cant get the votes to tally per node though.

My setup is the following ( using node comments).... Created new content type called Review with new field for the Fivestar, set options to 10 stars and pasted your code into the node ID.
Then create a page and set the node type for comments to Review.

I can make page and add reviews no problem but the tally doesn't work. I copy/pasted your static theme function right into the page body ( using php input format) as well as hard coding it into the themes tpl.php but I just get 5 empty stars.

teamrob’s picture

If youd like to have a look at test site login test/test http://dotnature.com/reviews/test/

sbandyopadhyay’s picture

Status: Closed (fixed) » Active

To echo teamrob, thanks for the code snippets quicksketch! But... I have the same exact problem as teamrob. Namely, the votingapi_get_voting_result function is not returning a valid rating. I poked around for a bit, and I've figured out a possible reason why: the votingapi_cache table is empty. This probably means that, for some reason, the vote averages are not getting tallied and cached properly.

Any help in addressing this would be greatly appreciated!

sbandyopadhyay’s picture

Version: 5.x-1.5 » 5.x-1.7
Category: support » bug

And i'm using Fivestar 1.7, and I believe teamrob may be using the same version as well

Will White’s picture

Same issue. Subscribing.

Will White’s picture

Category: bug » support

After a little debugging I found my problem. I hadn't enabled fivestar on my target content type. I just turned it on and set both "widget location" dropdowns to "hidden" so that the only way to vote is through a comment. Does that fix your problem as well?

teamrob’s picture

No this did not work for me:( Still getting blanks when turned on and set to hidden.

What did you put for the target in return $node->field_rating[0]['target']; I have mine set to "Star" which is the field name I made , is that correct?

Would love to get this to work.

lolabean’s picture

Hi Teamrob,

We're having the same kind of problem but I was hoping you could clarify your issue. Quickstretch pointed to a setup --
http://www.goodcharlotte.com/music -- which seems more or less like what we want to do. You may be doing what we're trying but I need some conceptual help.

If I get this correctly there is a difference between your setup and the goodcharlotte.com view. Yours is a node with a series of user comments (as nodes) created with node comment. The user comments all have ONE fivestar rating in them but you want the total for all that appear on that page only to tally on the page.? OR do you want all user reviews of that node to tally?

The goodcharlotte.com view is a series of stacked nodes -- either by views or custom code -- that are of a custom content type called reviews. So each song in the list is in fact a 'review' which is actually displaying just the song title. Users are able to rate the 'review' which is the song. This view does not need to have the total tallied as it would have no real meaning in this context. .

So you want to tally the reviews that the users have already entered that are displaying on the page. The Goodcharlotte view needs to allow on the spot ratings to the entered for each node. In other words rather than collecting the already entered ratings as in your scenario the user rates the nodes on that page.

Does this accurately reflect what you want and what is occurring on the Goodcharollte page?

My question to Goodstretch is exactly how is the content type and fivestar created for the songs that are listed on the page? Is it as you said a really simple CCK content type called 'review' that allows the admin to enter the song title and reveals the fivestar and the song title in the node?

I think I have this straight. Probably not.

teamrob’s picture

Well simply put the user review ( or comment) will show what the user rated for that node in thier comments and an average tally for them all will appear in the parent node, exectly like on the good charlotte website. This just shows a relation between the commenters review and the overall review, which can be useful for a more detailed scenario.

It doesn't make any difference if the its a custom content type, a page, a post .. or if its using default comments ,nodecomments, or any custom code. I don't think you fully understand how this works. I'm using nodecomments because its allows for a lot of customization, most of which is unrelated to fivestar.

The code posted above works great but there is a problem with the voting api that is either not storing the vote or displaying them when they are called, unfortuntly I haven't had time to look futher into why this is happening.

quicksketch’s picture

My question to Goodstretch is exactly how is the content type and fivestar created for the songs that are listed on the page?

Hahahaha, "goodstretch" I like that.

The list of tracks on that page is a teaser list of tracks, built with the views module. So it is actually the teaser display of each of the track nodes. node-track.tpl.php file is setup to output the voting widget for each node.

maxparm’s picture


Can you say us how to do the same things as http://www.goodcharlotte.com/music or http://www.daughtryofficial.com/music/album/daughtry step by step because I don't understand your previous messages.

Thanks in advance!

Kuldip Gohil’s picture

Title: Possible to use Fivestar with nodeComments? » I have to use Fivestar voting in my .tpl file


i have to print fivestar voting in my .tpl file
i am using jquery to node in custom view
i am print by using

$output= fivestar_widget_form($node);
print $output;

but it not working properly.. i cant do voting.

please give me solution

quicksketch’s picture

Title: I have to use Fivestar voting in my .tpl file » Possible to use Fivestar with nodeComments?
Status: Active » Closed (fixed)

Please do not hijack the issue with an unrelated request. I'm closing this issue again as we've solved the original request.

Minoo’s picture

Anonymous’s picture

Status: Closed (fixed) » Active

How would I use this with several fivestar fields (multi-axis)?

quicksketch’s picture

Status: Active » Closed (fixed)

txcrew, once again this would be a new support request. Though related to the original question, it's certainly different. Open a new issue and link to this one if necessary.

greg.harvey’s picture

Was a separate bug report raised about the empty votingapi_cache table? I can't find it - if not, I'll raise one. Not sure if it's a Fivestar issue or a VotingAPI issue though! =(

greg.harvey’s picture

Off original topic, but related to other content in the thread so worth posting:

While the Voting API team sort this out, if you set your Voting API settings to "Tally when cron runs" then the votingapi_cache table is populated correctly.

rimma’s picture

I have same problem. It looks like not working very well, just not sure what reasons. I have checked all possible reason, such as enable targeted content type five star, run cron. anything else.

swortis’s picture

Running cron does not work for me.

Anyone have any new suggestions for this issue?

greg.harvey’s picture

Try calling the function in question directly from your module - I can't remember what it is off the top of my head, but a quick review of the votingapi.module file should reveal it. It will have a sensible name, I'm sure. You could do it in a hook_nodeapi implementation just as a test.

swortis’s picture

That would be this?

function _votingapi_insert_cache_result($content_type, $content_id, $value, $value_type, $tag, $function) {
  $vobj->vote_cache_id = db_next_id('{votingapi_cache}');
  $vobj->content_type = $content_type;
  $vobj->content_id = $content_id;
  $vobj->value = $value;
  $vobj->value_type = $value_type;
  $vobj->tag = $tag;
  $vobj->function = $function;
  $vobj->timestamp = time();

  db_query("INSERT INTO {votingapi_cache} (vote_cache_id, content_type, content_id, value, value_type, tag, function, timestamp) VALUES (%d, '%s', %d, %f, '%s', '%s', '%s', %d)", 
    $vobj->vote_cache_id, $vobj->content_type, $vobj->content_id, $vobj->value, $vobj->value_type, $vobj->tag, $vobj->function, $vobj->timestamp);

  return $vobj;
JStarcher’s picture

Did anyone ever figure this one out? I've run into the exact same scenario.

smanes’s picture

Maybe I can help a little but I'll explain the circumstances and you figure out if it's useful to you. I'm building a dependent module for FeedAPI that provides a more structured view of RSS feeds. One of my content types displays a paged, formatted (i.e. banner image) list of teasers for the latest incoming feed posts. I want users to be able to vote on those posts, with the results aggregating back to the feed's "home page". In other words, the feed's home page (a FeedAPI "feed" type) will collect the average of all the votes for all of its feed items.

With the latest FiveStar and VotingAPI modules for v6, it was actually cake. In the theme preprocessing function for the feed teaser:

    $content = [extracted teaser text...]
    $parent = node_load([some code to grab the parent feed's nid]);

    if (module_exists('fivestar') && $widget = fivestar_widget_form($parent)) {
        $content .= $widget;

Works great.

pdenoeud’s picture

I have one question related to this issue.
quicksketch said:
"To display the combined rating on the target node, display the static theme function:

  $current_rating = votingapi_get_voting_result('node', $nid, 'percent', 'vote', 'average');
  print theme('fivestar_static', $current_rating->value);


With Drupal 7, can we display this combined rating in a view that would list the target nodes, so that we can display in the view the five star widget for each row, and eventualy sort the list by rating value?
The use case is to allow reviews of hotels, and display the list of all reviewed hotels, with their rating. Users could then sort the hotels by rating to see first the one with the best rates.

I guess it depends if each hotel's combined rating is stored in database (and updated each time a review is added/edited/removed), or if it is caculated on the fly when we call the votingapi_get_voting_result method.