Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save MisterMike/538dbbb942085f5bf387d1ac2c4965f6 to your computer and use it in GitHub Desktop.
Save MisterMike/538dbbb942085f5bf387d1ac2c4965f6 to your computer and use it in GitHub Desktop.
Craft CMS: Filter entries by category of QueryString is found
{# get url parameter 'filter' #}
{% set filterQuery = craft.request.getParam('filter') %}
{# if we have a query string, select these categories and find related entries #}
{% if filterQuery|length %}
{% set relatedCats = craft.categories.group('verguenstigungen').slug(filterQuery) %}
{# setup search parameters #}
{% set searchParams = {
relatedTo: relatedCats,
section: 'verguenstigungen'
} %}
{# Find all entries related to categories #}
{% set entries = craft.entries(searchParams).order('score') %}
{# if we have no query string, find categories in category group #}
{% else %}
{% set relatedCats = craft.categories().group('verguenstigungen') %}
{# get all related entries #}
{% set entries = craft.entries({
section: 'verguenstigungen',
orderBy: 'title asc, elements.id asc',
limit: 10
}) %}
{% endif %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment