1

I have 2 arrays of entry ID's that I need to merge and generate an entries object that I can iterate through and paginate. The reason for doing it this way at all, is that I need to display entries with a specific category, and those without any category as per the code below.

Perhaps I'm approaching this all wrong and there's a different way to get the entries.

{% set vetCategoryID        = 437950 %}
    {% set noPersonnelType  = craft.entries.section('veterinarians').personnelType(':empty:').limit(null).ids() %}
    {% set vets             = craft.entries.section('veterinarians').personnelType(vetCategoryID).limit(null).ids() %}
    {% set merged           = noPersonnelType|merge(vets) %}
    
    {# this displays all, but makes pagination impossible, so no dice. #}
    {% for item in merged %}
        {% set entry = craft.entries({id: item}) %}
        {{ dd(entry) }}
    {% endfor %}

I tried using {% set entries = craft.entries(merged) %} but that doesn't work at all.

Searching here gives me this question: Converting array of entries to an array of ids, but it's the opposite problem.

2 Answers 2

3

If I understand correctly, you could do this in one query:

{% set vetCategoryID = 437950 %}

{# Get all entries where personnelType is empty OR equals vetCategoryID #}
{% set query  = craft.entries()
    .section('veterinarians')
    .personnelType([':empty:', vetCategoryID])
    .limit(10) %}

{# Paginate #}
{% paginate query as pageInfo, pageEntries %}

{% for entry in pageEntries %}
{{ entry.title }} <br>
{% endfor %}

{% if pageInfo.prevUrl %}<a href="{{ pageInfo.prevUrl }}">Previous Page</a>{% endif %}
{% if pageInfo.nextUrl %}<a href="{{ pageInfo.nextUrl }}">Next Page</a>{% endif %}

See documentation on querying Categories fields and pagination.

1
  • You're right, that's much better and won't need explanatory variable names. Thanks. Funny enough, I was reading that part of the doc and when I saw :empty: my brain went "Yea, that's all we need, no need to read anymore" :-D Mar 9 at 13:32
1

The answer to this was simpler than I thought. So simple, that I thought I had tried it already.

{% set entries          = craft.entries.id(merged) %}

    {% for entry in entries %}
        {{ entry.id }}: {{ entry.title }}
    {% endfor %}

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.