When the (core) statistics module is enabled, we get a '50 reads' at the bottom of each node.

Is there any way I can pull that '50' (the node view count) and print it in a node template?

I want to accomplish this without a contribute module.

Any ideas?

  • What are you using for display? As the data is being printed you should not need a contrib module (assuming you already have Devel on your local, which you should because it's awesome). I would start using dpm on your theme_preprocess_node and page. If this doesn't work please give a little more information on your set up. Such as panels/blocks/views etc.
    – sareed
    Apr 21, 2015 at 16:41
  • @sareed - Not using panels or blocks for this, I simply want to print the node hits statistic that the core statistics module provides in a node--type.tpl.php :) is that clear enough? any ideas?
    – caustic
    Apr 21, 2015 at 16:44
  • @sareed - the core statistics module prints the hits of a node at the bottom of each node like '50 reads'. i just want to know how I can print that counter myself wherever I want in the node template
    – caustic
    Apr 21, 2015 at 16:46

1 Answer 1


The statistics module provides that data through statistics_get(), which returns

An associative array containing:

  • totalcount: Integer for the total number of times the node has been viewed.
  • daycount: Integer for the total number of times the node has been viewed "today". For the daycount to be reset, cron must be enabled.
  • timestamp: Integer for the timestamp of when the node was last viewed.


$stats = statistics_get($nid);
$total_count = $stats['totalcount'];
$day_count = $stats['daycount'];

To get it into a node template, implement hook_preprocess_node(), hook_node_view_alter(), or hook_node_view() (the latter will need to be in a custom module, the other 2 can go in a theme or module).

hook_preproces_node() example:

function MYTHEME_preprocess_node(&$vars) {
  $stats = statistics_get($vars['node']->nid);
  $vars['stats_total_count'] = $stats['totalcount']; 

And in the template

<?php echo $stats_total_count; ?>

You'll need to clear Drupal's cache after adding the hook so the theme registry picks it up.

  • Done, fortunately it's a pretty simple thing to accomplish. The hook goes in template.php in the theme by the way (not sure how much you know about theming)
    – Clive
    Apr 21, 2015 at 16:58
  • Thank you Clive; but I get these errors after flushing Drupal cache: Notice: Undefined variable: nid in theme_preprocess_node() Notice: Undefined variable: stats_total_count in include()
    – caustic
    Apr 21, 2015 at 17:01
  • Didn't update the var for context, the edited version should be ok
    – Clive
    Apr 21, 2015 at 17:05
  • Now I'm getting: Notice: Undefined variable: vars in theme_preprocess_node and Notice: Trying to get property of non-object in theme_preprocess_node... wonder what is wrong ... I appreciate the help
    – caustic
    Apr 21, 2015 at 17:10
  • I got it working! Thank you (so much!!!!!!) Clive! You've taught me a thing or two :)
    – caustic
    Apr 21, 2015 at 17:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.