Counting Flags within the API

Last updated on
30 April 2025

Here is one method for outputting the number of flags placed on an item using the API.

A counter field is updated whenever an item is flagged or unflagged. This counter can be read using the flag object's get_count() method.

To start, use flag_get_flag() to get the relevant flag object. Then use get_count() to tally number of times the specific type of content has been flagged with this flag. For example, to get the count of a node-based flag, named "votes":

  // Get the flag object.
  $flag = flag_get_flag('votes');
  print "The number of people who voted for this proposal is: ";
  // Get the flag count.
  print $flag->get_count($nid);

In the example, $nid is the node ID of the flagged node. If the flag is user-based, then the $uid of the flagged user must be passed to the get_count() function. For flags on other entities, use the appropriate id types.

This was tested in node.tpl.php.

Help improve this page

Page status: Not set

You can: