Find within a collection

I have a big content folder full of authors and their books. Structured as you’d expect:

01-writers/
   george-orwell/
      1984/
      animal-farm/
   thomas-pynchon/
      gravitys-rainbow/
   ...

Elsewhere on the site, there is a “reading lists” section. The reading lists each have an array of UIDs of books that they contain. Given gravitys-rainbow, why does this not work?

<?php $books = $site->pages()->find('writers')->children()->children();
	$a_book = $books->find('gravitys-rainbow');
	echo $a_book->title();
?>

Should I be using the search() function? It seems like overkill - and I can’t seem to limit it to only the UID anyway.

I feel like I’m missing something super basic here?

Without actually testing this code, that children()->children() looks problematic. I think children() needs to be chained to a single page.

What about this:

$site->pages()->find('writers')->children()->index()->filterBy('template','book')
1 Like

That’s exactly what I needed! Thanks!

Edit:

It looks like I may still be using find() incorrectly, or it’s not the proper solution -
but this works:

$all_books = $site->pages()->find('writers')->children()->index()->filterBy('template','book');

echo $all_books->filterBy('uid', 'gravitys-rainbow')->first()->title();

There is also $pages->grandChildren:

<?php

$all_books = page('writers')->grandChildren();
$gravitys_rainbow = $all_books->findBy('uid', 'gravitys-rainbow');

I have never used find, but always findBy. The only problem with this solution is that you don’t filter by the book template.

1 Like

findBy makes much more sense and I had forgotten about grandchildren also… time to give the docs a good read-through… or sleep