Count number of pages with an attribute?1 user found helpful
How would I efficiently count the number of pages that have a particular attribute? Ideally using the API (although whatever works is fine!)
I want to use the number to amend another page count (num of children - number of children with attribute X)
Drawback -- you have to hand count the rows.
So nice answer Doki.
It does display the total number of pages at the top when you search the pages, so no reason to count one by one. : )
This even works for page types...
Loader::model('page_list'); $pl = new PageList();
The file for this model is at /concrete5/models/page_list, so if you want to look at all the methods involved.
You can get the count at any time. The ability to use filters and sorting are also involved. Filter by name, alias, sort by date, filter by collection type id, by handle, or set your own query. This is complete OO so I believe this is the API route you wanted to go. The above example will have all the pages that are in the system.
echo $pl->getTotal(); //display 15 which matches my sitemap count (no system pages)
I'm actually trying to edit Matogertel's Search Tools addons (specifically the Tag Cloud block) to make it show tag counts for pages only when they have a specific attribute set ('active').
I looked at the code of the controller for the addon but couldn't work it out, so I thought I would hack at it by subtracting the number without the attribute set from the total number, thereby leaving an accurate count. Messy but it would work. That said, I think i'll keep going with trying to edit the block's functionality directly. Matogertel's not very quick with support queries though, so I'm a bit in the dark.
Thanks for your suggestions & time spent looking - much appreciated! :)
I tried the following, to see if I could filter by the select attribute on its own:
Loader::model('page_list'); $pl = new PageList(); $pl->filterByAttribute('attr_name','attr_value','like'); echo $pl->getTotal();
..but no joy. I think this is related to a known bug re spaces being inserted into the array of attribute values, but i'm not sure to what extent that affects this.
This should solve it.
This worked.. I was able to filter first by pages with the active attribute, then by the given select attribute to get the right count:
$currentSector = '%'.$value['word'].'%'; $pl = new PageList(); $pl->filterByAttribute('job_active',true,'='); $pl->filterByAttribute('sector',$currentSector,'like'); $sectorTotal = $pl->getTotal();
Thanks again :)
I took 15 minutes to spin through the APIs, but I didn't see anything like that. (Reference: you can see the complete list of APIs at:http://www.concrete5.org/api/).... The only think I can think of at the moment is to do several API calls, one to get a list of all cIDs (aka the id number for every existing page), get a list of the maximum cvIDs for each cID (aka get the latest page version) then run something to the extent of:
Loader::model('attribute/categories/collection'); $attributes = CollectionAttributeKey::getAttributes($cID, $cvID, [ $method = 'getValue']);
..for each cID/cvID. Search the list for the akHandle of the attribute you're interested in (aka, if you want Exclude from Nav, look for 'exclude_nav' as the akhandle). Store if found. Rinse, repeat until list of cIDs/cvIDs are exhuasted. That should get you your list.
Sorry I couldn't find an easier method. Hopefully someone else can build on this to get a simpler answer....