Get pages where a checkbox is checked

Hi, wondering how to do this, not something I have tried before.

If I add checkbox to my blog blueprint to indicate it is a featured post:

postoptions:
label: Post Options
type: checkboxes
options:
  featured: Featured 

I know I can use something like this to get the posts from my blog

$posts = $page->children()->listed()->flip();

What I am not sure of is how I would amend the above to only return those posts where the checkbox I have just added is checked.

Any help appreciated, I have look at the docs but can’t figure it out. #newbie

Thanks

Sorry, I did have it figured, turns out I didn’t save the only post where I checked the checkbox!

$featured = $page->children()->filterBy('postoptions', 'featured', true);

The answer above just in case anyone else needs it.

Since your are using a checkboxes field, this will store the value featured when checked, not true. So the third option on your filterBy is actually superfluous and misleading, should be

$featured = $page->children()->filterBy('postoptions', 'featured');

As a third option you could pass the separator in this example, which isn’t necessary here.

1 Like