Skip to content

Instantly share code, notes, and snippets.

@jpdevries
Last active August 29, 2015 14:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpdevries/c6c80d27a622d2de946f to your computer and use it in GitHub Desktop.
Save jpdevries/c6c80d27a622d2de946f to your computer and use it in GitHub Desktop.
get the hit count of a single MODX resource
<?php
$defaultHitsCorePath = $modx->getOption('core_path').'components/hits/';
$hitsCorePath = $modx->getOption('hits.core_path',null,$defaultHitsCorePath);
$hitService = $modx->getService('hits','Hits',$hitsCorePath.'model/hits/',$scriptProperties);
if (!($hitService instanceof Hits)) return 'failed';
$s= '';
// pass this into your snippet [[!getHitCount? &resource=`[[**id]]`]]
$resource = modx->getOption('resource',$scriptProperties,$modx->resource->get('id'));
$c = $modx->newQuery('Hit');
$c->where(array(
'hit_key:IN' => $resource
));
$c->limit(1);
$hit = $modx->getOne('Hit',$c);
// assume there's a hit_count chunk containing: [[+hit_count]]
$s .= $modx->getChunk('hit_count',$hit->toArray());
return $s;
@wjthomas9
Copy link

I just seem to get a white screen with this. I noticed you are missing a $ in front of modx in line 11.

I'm also not familiar with the [[**id]] syntax. I thought it was just a sing le asterisk. I'm also wondering how you would use this in an element in a getResources chunk.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment