Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save conschneider/01ff8f421d63a14b9deef136a5a0d30b to your computer and use it in GitHub Desktop.
Save conschneider/01ff8f421d63a14b9deef136a5a0d30b to your computer and use it in GitHub Desktop.
Filter a collection by taxonomy tag and exclude entries with a specific taxonomy.
<?php
namespace Statamic\Addons\StartTax;
use Statamic\Extend\Filter;
class StartTaxFilter extends Filter
{
/**
* This excludes entries that have a specific tag set. In this case 'Startseite'.
*
* @return \Illuminate\Support\Collection
*/
public function filter($collection)
{
return $collection->filter(function ($entry)
{
$entry_tag = $entry->get('tags'); // looks for tags array.
if(empty($entry_tag)) // if no tags are set, no 'Startseite' is set as well so output.
{
return $entry;
}
// if there are tags, we need to know if 'Startseite' is active.
if(!empty($entry_tag)) {
$check_for_startseite = in_array('startseite', $entry_tag); // check if tag 'Startseite' is set.
// If no tag 'Startseite' is set output the entry.
if(!$check_for_startseite) {
return $entry;
}
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment