1

I have an Entries field so I can select a number of posts to feature in a "Hero" section on the home page, as well as a "most popular" section underneath it.

However, the Hero posts keep showing up in this section, so I'd like to exclude them from the query.

Is there a way I can get the array of entry id's from the Hero section and insert it into the popular query?

Am using the Trendin Posts plugin (https://github.com/madebyraygun/trending-posts) to get the popular posts.

{% set entries = craft.entries().section('news').limit('4').orderBy('popular').all() %}

and I would like...

{% set entries = craft.entries().section('news').limit('4').orderBy('popular').id('not', {array of IDs from Hero}).all() %}

1 Answer 1

3

At the same time as you are iterating over your Hero posts, you could populate an array that you could then use in your most popular query.

This is just sudo code but hopefully you get the idea:

{# This would go before your hero loop #}
{# Initiate a heroIds array starting with not #}
{% set heroIds = ['not'] %}

{% for entry in heroEntries %}
    {# Add  heroEntries id to your heroIds array #}
    {% set heroIds = heroIds | merge([entry.id]) %}

    {# the rest of your code... #}
{% endfor %}

{# Then you can use this array in your most popular query #}
{% set entries = craft
    .entries()
    .section('news')
    .limit('4')
    .orderBy('popular')
    .id(heroIds)
    .all() %}
1
  • Worked a treat! Thanks.
    – MarkD
    Commented Jul 15, 2020 at 6:10

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.