Skip to main content
2 of 4
added 2904 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: Getting a lot closer

Updated Stash to 2.4.3. and refactored using just append_list and adding another parameter to be able to sort the lit when I get it. Works great but I cannot for the life of me work out how to do multiple orderby and sort here.

{!-- Extended page template --}
{stash:embed name="layouts:page"}

{!-- 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:playa:children channel="articles|interviews" limit="4"}

        {exp:stash:append_list name="st_complementary_items"}
            {stash:st_complementaryitem_title}{title}{/stash:st_complementaryitem_title}
            {stash:st_complementaryitem_type}playa{/stash:st_complementaryitem_type}
            {stash:st_complementaryitem_date}{entry_date format="%F %d, %Y"}{/stash:st_complementaryitem_date}
            {stash:st_complementaryitem_date_machine}{entry_date format="%Y-%m-%d"}{/stash:st_complementaryitem_date_machine}
        {/exp:stash:append_list}

    {/exp:playa:children}

{/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}
        {stash:st_complementaryitem_type}recent{/stash:st_complementaryitem_type}
        {stash:st_complementaryitem_date}{entry_date format="%F %d, %Y"}{/stash:st_complementaryitem_date}
        {stash:st_complementaryitem_date_machine}{entry_date format="%Y-%m-%d"}{/stash:st_complementaryitem_date_machine}
    {/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="st_complementaryitem_title"}</p>

        {!-- get the list of related items (Playa + recent), order by playa first, order entries inside each group by date (@TODO make it unique on the title, which works) --}
        {exp:stash:get_list name="st_complementary_items" orderby="st_complementaryitem_type|st_complementaryitem_date_machine" sort="asc|desc"}
            <p>[{count}/{total_results}] - {st_complementaryitem_title} - {st_complementaryitem_date} - {st_complementaryitem_type}</p>
        {/exp:stash:get_list}

{/exp:stash:set}