1

I have a channel called 'people' where a dropdown is used to say if they like 'cricket' or 'football'

Inside a matrix related entry I am trying to show what they like from that dropdown field value inside a block for 'cricket' and block for 'football'

{% set relatedPeople = craft.entries.section('people').relatedTo(block).all() %}

This gets the channel but I am struggling to then filter by the dropdown value, I thought I could use relatedTo but that's not for dropdown values.

I tried this on the group I created for the dropdown

relatedPeople.peopleGroup('cricket')) 

I get:

Impossible to invoke a method ("peopleGroup") on an array.

Any help would be grateful

1 Answer 1

2

Relations won't work here because they only apply to elements (Entries, Categories, Assets, etc). Instead, you can use the field handle to filter by field values depending on the field's type. See, for instance, the documentation of Dropdown Fields to find out how to filter them. In this case, your approach is correct in principle, you can pass a string that matches the value of one of the dropdown options to search for entries with that option selected.

So this should work:

{% set relatedPeople = craft.entries()
    .section('people')
    .peopleGroup('cricket')
    .all()
%}

I get - Impossible to invoke a method ("peopleGroup") on an array.

This is just a minor error, the problem is that you're trying to add a condition after you've already executed the query using .all(). The .all() method returns an array of results, so trying to call a method on that array will result in an error.

If you can't add all conditions in a single expression as I did above - for example, because some conditions are only added in specific situations - make sure only to execute .all() after all conditions have been added. Something like this:

{# Create a query (craft\elements\db\EntryQuery). #}
{% set relatedPeopleQuery = craft.entries().section('people') %}

{# Add more conditions to the query. #}
{% do relatedPeopleQuery.peopleGroup('cricket') %}

{# Execute the query and fetch the results. #}
{% set relatedPeople = relatedPeopleQuery.all() %}
3
  • Thanks for the detailed answer, it works great. I have an understanding of how Craft CMS works in regards to the issue. It makes sense now.
    – smudge18
    Commented Sep 13, 2021 at 9:01
  • @smudge18 Glad I could help! Would you mind marking my answer as accepted then? Thanks!
    – MoritzLost
    Commented Sep 13, 2021 at 12:05
  • 1
    All done.. thanks for your help!
    – smudge18
    Commented Sep 14, 2021 at 18:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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