1

I am currently searching within multiple sections on my site.

I'd like to limit one of the sections (courses) to a specific entry type (cce)?

Any help would be greatly appreciated!

{% set params = { 
    section: [ 'pages', 'careerPaths', 'courses', 'events', 'eventsIndex', 'news', 'newsIndex', 'programs', 'allPrograms', 'facultyStaff', 'facultyStaffIndex' ], 
    limit: siteVariables.resultsLimitForPagination 
} %}

{# if search term exists, search #}
{% set searchTerm = craft.app.request.getParam('q') %}

{% if searchTerm|length %}
    {% set params = params|merge({ groupBy: ['title'] }) %}
    {% set params = params|merge({ search : searchTerm, orderBy: 'score DESC'}) %}
{% endif %}

{# Query #}
{% set query = craft.entries(params)  %}

{% paginate query as pageInfo, entries %} 
1

I suspect the only way to do this at the moment would be to look up the EntryTypes for each of the desired Sections, and filter just by those—ignoring the section query facet. For example:

{# Map some Section handles into actual Section models, using the Sections Service: #}
{% set sections = ['one', 'two', 'three'] | map((h) => craft.app.sections.getSectionByHandle(h) %}

{# List your "forbidden" Entry Types in a `sectionHandle:entryTypeHandle` format, so we can filter them out, in a moment: #}
{% set forbiddenEntryTypes = [
  'mySection:entryTypeOne',
  'mySection:entryTypeTwo'
] %}

{% set entryTypes = [] %}

{% for section in sections %}
  {# Get the section's EntryTypes, filtering out any that don't match our "magic" `section:entryType` values in the "forbidden" array, above: #}
  {% set permittedSectionEntryTypes = section.getEntryTypes() | filter((t) => "#{section.handle}:#{t.handle}" not in forbiddenEntryTypes) %}

  {# Merge anything that passes the test into the main `entryTypes` array: #}
  {% set entryTypes = entryTypes | merge(permittedSectionEntryTypes) %}
{% endfor %}

{% set query = craft.entries({
  type: entryTypes
}) %}

This basically works by assuming you want all types from all sections, then filtering out just a few types from that list.

For example, your forbiddenEntryTypes might look something like this, if you had three other Entry Types in the Courses section…

{% set forbiddenEntryTypes = [
  'courses:night',
  'courses:takeHome'
] %}

…where cce is allowed, because it's returned by the section, but not explicitly filtered out. You'll have to update this if/when you add more Course types!

Keep in mind that the sectionHandle:entryTypeHandle format is not a Craft feature, it's just a way for us to compare Section + Entry Type handles at once (note the way we build the "#{section.handle}:#{t.handle}" string when filtering down the Entry types)!

1

Thank you for the help August!

I was able to get this work as intended. The only issue we ran into was getting an error for {type: entryTypes} when it was an an array of EntryTypes.

Here is the finished code:

{% set sections = [ 'pages', 'careerPaths', 'courses', 'events', 'eventsIndex', 'news', 'newsIndex', 'programs', 'allPrograms', 'facultyStaff', 'facultyStaffIndex' ] | map((h) => craft.app.sections.getSectionByHandle(h)) %}

{# List your "forbidden" Entry Types in a `sectionHandle:entryTypeHandle` format, so we can filter them out, in a moment: #}
{% set forbiddenEntryTypes = [
    'courses:standard',
    'courses:cceCertificate'
] %}

{% set entryTypes = [] %}

{% for section in sections %}
    {# Get the section's EntryTypes, filtering out any that don't match our "magic" `section:entryType` values in the "forbidden" array, above: #}
    {% for entryType in section.getEntryTypes() | filter((t) => "#{section.handle}:#{t.handle}" not in forbiddenEntryTypes) %}
        {% set entryTypes = entryTypes | merge([entryType.handle])%}
    {% endfor %}

{% endfor %}

{# dd entryTypes #}
{% set params = params|merge({ type: entryTypes }) %}
1
  • Nice! Alternatively, as you merge the value in with you query, you could use entryTypes | column('id') so that you're not discarding any actionable info until the last moment—using id may also prevent additional queries, as the handle ultimately needs to get mapped to an ID anyway! (This may well be cached/memoized on the Sections service…) Nov 17 '20 at 20:35

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.