Query structure fields

How can I iterate through a structure where a specific field has a specific value?

Example, I have structure setup like that in yml:

      jobs:
        label: Jobs
        type: structure
        fields:
          enabled:
            label: Enabled
            type: toggle
            default: true
          title:
            label: Job
            type: text

In the template I want to iterate through all “enabled” job positions like:

	<?php foreach( $page->jobs()->toStructure()->filter(['enabled' => 'true']) as $job ): ?>
		<?= $job->title() ?>
	<?php endforeach; ?>

Use filterBy() like this:

<?php foreach( $page->jobs()->toStructure()->filterBy('enabled', '==', 'true') as $job ): ?>
	<?= $job->title() ?>
<?php endforeach; ?>

:wink:

1 Like