Fetching all pages with taxonomy in plugin


I’m trying to iterate through all pages with a specific tag. Right now, my code looks like this:

$pages = \Grav\Common\Grav::instance()['taxonomy'];
$pages =  $pages->findTaxonomy(['tag']);

The docs dictate that the Taxonomy class’ findTaxonomy function Returns a new Page object with the sub-pages containing all the values set for a particular taxonomy.

So, that makes me think that I need to utilize the Page class’ children function to fetch the pages like so:

$children = $pages->children();

And I would then be able to iterate through the pages and treat those as Page objects.

But running a foreach loop over $children yields an error (An exception has been thrown during the rendering of a template (“Invalid argument supplied for foreach()”).). Looping over $pages yields nothing. The tag I search for has been assigned to pages for testing.

To be perfectly honest, I’m kinda fumbling in the dark. Is the taxonomy class even assigned correctly? What am I doing wrong?


The Random plugin shows how they deal with it. You may find that helpful.