6

I've came across a problem today where I wanted to 'inject' a stash pipe separated list of entry_ids into the entry_id parameter of a channel entries loop.

The problem wasn't to do with stash as such. I'd got my parse order correct, and stash was doing it's job as expected. The problem was that when my stash variable didn't contain any entry_ids, the channel_entries loop just output the latest entries in that channel rather than output nothing.

Here's my initial code that didn't do what I wanted:

{exp:stash:set name="recently_viewed_ids" output="no" parse_tags="yes" trim="yes"}
{exp:channel:entries channel="properties" disable="categories|member_data|pagination|trackbacks" dynamic="no" backspace="1"}{entry_id}|{/exp:channel:entries}
{/exp:stash:set}

and the channel entries loop that used the stash variable:

{exp:channel:entries 
    channel="properties"
    entry_id="{exp:stash:get name='recently_viewed_ids'}"
    limit="4"
    disable="categories|member_data|pagination|trackbacks" 
    parse="inward"
}
{!-- display custom fields here --}
{/exp:channel:entries}

So even if recently_viewed_ids was empty, EE returned the 4 latest entries in the channel, rather than nothing.

So what's the way around this?

Jim

5

After much searching and head scratching I finally turned to Twitter and got help from the fabulous @mediagirl and @low.

The solution to this is fairly simple:

To trigger a 'no_results' situation in the channel_entries loop using the entry_id parameter we have to enter either "-1" or "0|":

entry_id = "0|"

In my case I used "0|" and actually fixed_order instead of entry_id to preserve the order that the entry_ids are in.

So my final working code now looks like this:

{exp:channel:entries 
    channel="properties"
    fixed_order="0|{exp:stash:get name='recently_viewed_ids'}"
    limit="4"
    disable="categories|member_data|pagination|trackbacks" 
    parse="inward"
}
    {!-- display custom fields here --}
{/exp:channel:entries}

I hope that this manages to help someone else.

Incidentally, I should add that I'm using the JP Recently Viewed extension, so my original channel_entries loop also contains recently_viewed="y".

Cheers

Jim

| improve this answer | |
  • Exactly right. I've used "0|" many times to solve this problem, it's a neat trick. Thanks for taking the time to share this! – Adrian Macneil Feb 21 '13 at 15:16
  • Hi Jim, thanks for posting your fix - solved my problem after 2 days of trying. Also, you may not need JP recently viewed if you put this on your single entry page. {exp:stash:set scope="user" save="yes" name="recently-viewed" replace="yes" parse_tags="yes"}{exp:stash:get name name="recently-viewed" scope="user"}{entry_id}|{/exp:stash:set} – user1070143 Feb 26 '13 at 19:33
  • Hey @user1070143 that stash technique looks amazing! Will that only work for logged in users or is it cookie based as well? – Jim Pannell Feb 27 '13 at 7:01

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.