TransWikia.com

Help with an Entries field to an array I can use elsewhere

Craft CMS Asked on September 4, 2021

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

One Answer

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

Correct answer by Oli on September 4, 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