Count pages with attribute (tag) assigned

Permalink
Hi,
how can I retrieve the number of pages that have an attribute (a tag) applied to? In previous version I used this function that in 8.10 seems not working.
thanks

public function pageUseCount($optID) {
      $db = Loader::db();
      $q = "select Pages.cID from Pages where cIsActive = 1";
      $r = $db->query($q);
      if ($r) {
         while ($row = $r->fetchRow()) {        
            $page = Page::getByID($row['cID']);
            if (gettype($page->getAttribute($this->attributeHandle)) == 'object') {
               foreach($page->getAttribute($this->attributeHandle) AS $v) {
                  if ($v->ID == $optID) $i[$v->ID][] = true;
               }
            }
         }
      }
      return count($i[$optID]);