1

I have a filter with a few different parameters that get pulled in and I was wondering if there was a relatedTo wildcard that I could use.

Here's the relevant code:

{% set function = craft.app.request.getParam("function") %}

{% set functionSection = 'craft.categories.group('businessFunction')'  %}

{% if function | length %}
    {% set functionSection = craft.categories.group('businessFunction').slug( function|split(',') ) %}
{% endif %}


{% set posts = craft.entries({
          section: category,
          relatedTo: [
              'and',
              { element: functionSection } 
          ]
}) %}

The problem is if an entry doesn't have any Business Function selected then it's not related. I could have a conditional that ignores the relationship but there's multiple relations in the real code - it'd be easier if I could set functionSelect equal "all" or something similar.

1

I had to use a bunch of conditionals to accomplish this - it's not ideal but here's what I came up with

{% set category = craft.app.request.getParam("category") %}
{% set function = craft.app.request.getParam("function") %}
{% set industry = craft.app.request.getParam("industries") %}

{% if not category | length %}
        {% set category = "blog,caseStudies,videos,webinar,whitePapers" %}
{% endif %}

{% set category = category | split(',') %}


{% set posts = craft.entries({
    section: category,
    orderBy: "postDate desc"
}) %}

  {% if function | length %}
    {% set functionSection = craft.categories.group('businessFunction').slug( function|split(',') ) %}
    {% set posts = posts.relatedTo(functionSection) %}
  {% endif %}

  {% if industry | length %}
    {% set industrySection = craft.categories.group('industry').slug( industry|split(',') ) %}
    {% set posts = posts.relatedTo(industrySection) %}
  {% endif %}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.