TransWikia.com

Specify a specific entry type in paramaters

Craft CMS Asked by weslawson on June 24, 2021

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 %} 

2 Answers

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)!

Correct answer by August Miller on June 24, 2021

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 }) %}

Answered by weslawson on June 24, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP