Skip to main content
4 of 4
added 4 characters in body

Stash: list of related entries completed by recent entries

What I am trying to do:

  • I am in an entry detail page.
  • Entries have a playa field to create relationships w/ other entries
  • First, I need the first 4 related entries, in the order specified by the user in the Playa field
  • Then, I need to add the 4 most recent entries to that list
  • Finally, I need to display the first 4 entries of that list (Playa entries + Recent entries) and filter out duplicates

3 possible scenarios

  • I have no Playa entries > display just the 4 most recent entries
  • I have 4 Playa entries > just display those
  • I have a mix of Playa entries and Recent entries. In that case, Playa entries have to be displayed first in the order specified by the user

Here is what I have tried so far

{!-- SET Entry data --}
{exp:channel:entries disable="member_data|pagination|trackbacks" channel="articles" status="not closed" require_entry="yes" limit="1"}

    {!-- Get first 4 related items in Playa field (combined with latest article and interviews if less than 4 --}
    {exp:stash:set_list name="st_complementary_items" parse_tags="yes"}
        {exp:playa:children channel="articles|interviews" limit="4"}
            {stash:st_complementaryitem_title}{title}{/stash:st_complementaryitem_title}
        {/exp:playa:children}
    {/exp:stash:set_list}

{/exp:channel:entries}


{!-- Get last 4 published articles / interviews items (completes the list if less than 4 in the Playa field  --}
{exp:channel:entries disable="member_data|pagination|trackbacks" channel="articles|interviews" status="not closed" orderby="date" sort="desc" dynamic="no" limit="4"}

    {exp:stash:append_list name="st_complementary_items"}
        {stash:st_complementaryitem_title}{title}{/stash:st_complementaryitem_title}
    {/exp:stash:append_list}

{/exp:channel:entries}


{!-- BUILD page content --}
{exp:stash:set name="content"}

    <p>Total items in list: {exp:stash:list_count name="st_complementary_items"}</p>
    <p>Unique items in list: {exp:stash:list_count name="st_complementary_items" unique="yes"}</p>

    {!-- get the list of related items (Playa + latest if less than 4 playa), still have to make rows unique using the unique=yes parameter --}
    {exp:stash:get_list name="st_complementary_items"}
        <p>[{count}/{total_results}] - {st_complementaryitem_title}</p>
    {/exp:stash:get_list}

{/exp:stash:set}

That works decently well, unless I have no playa items. Then Stash acts as if I have 5 items in my list with a first one that's not parsed.

Total items in list: 5 / Unique items in list: 5

  • [1/5] - {st_complementaryitem_title}
  • [2/5] - Recent Entry title
  • [3/5] - Recent Entry title
  • [4/5] - Recent Entry title
  • [5/5] - Recent Entry title

I am at loss as to why this is happening.

If I completely remove the recent entries loop and just leave the Playa loop, things work as expected and I get 0 entries.

Grateful if anyone can help me out with that.

EDIT: Mark "The" Croxton was quick again: Stash 2.4.4 has been updated to ignore empty list items which apparently was the problem here.