Created
March 25, 2019 16:10
-
-
Save MisterMike/538dbbb942085f5bf387d1ac2c4965f6 to your computer and use it in GitHub Desktop.
Craft CMS: Filter entries by category of QueryString is found
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{# 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