Reconfiguring a StarRating snippet - php help needed!

I’m looking to reconfigure a StarRating snippet to suit a website redesign which I’m currently embarking on.

The snippet displays a list on my home page of the top 10 best rated circuits on However, I’ve spotted a couple of issues:

  1. It’s currently configured to count results only from resources using template 17, when it should really show from resources which use templates 13 or 17;
  2. I’d like to additionally like to set placeholder values for the pagetitle and a further template variable (named circuit_countrycode) of the parent resource.

I’m not well versed in php and didn’t write the original, so would appreciate any help!

Current snippet code below:

    $showhidden = isset($showhidden) ? $showhidden : 0;
    $limit = isset($limit) ? $limit : 10;

    $snippetPath = $modx->getOption('core_path').'components/star_rating/';
    //$manager = $modx->getManager();

    $c = $modx->newQuery('modResource');
    $c->leftJoin('starRating','starRating', array(" = starRating.star_id"));

    $c->select(', vote_total/vote_count AS rating');
    $where = array(
         'modResource.published' => '1',
         'modResource.template' => '17',
         'modResource.hidemenu' => $showhidden
    if (isset($parent)) {
    if (isset($template)) {
    //$c->prepare();echo $c->toSQL();
    $ratings = $modx->getCollection('modResource', $c);
    foreach ($ratings as $rating) {
    return $output;

I hope you don’t mind – I added three back-ticks above and below your code to preserve the format.

For starters, back up the snippet, then see if this works to get resources with the other template:


'modResource.template' => '17',


'modResource.template:IN' => array('13','17'),
1 Like

Add these lines to the foreach loop to load the parent resource and read the TV.

//existing foreach loop
foreach ($ratings as $rating) {
    //new code
    $parent = $modx->getObject('modResource', $rating->get('parent'));
    if ($parent) {
        $countrycode = $parent->getTVValue('circuit_countrycode');

Many thanks - I was wondering how to do that. Has been a while since I posted on the Modx forums and the first since the new format launched!

Thanks Bob - that does indeed work.

Brilliant - that works a treat. Thanks very much for your help!