3

I have a three different statuses displaying within one main channel entries wrapper. I would like the first status to be ordered randomly, and the other two statuses will be sorted alphabetically by title. The code below obviously does not work, but it conveys what we are looking for. Can anyone help with this?

{exp:channel:entries status="1|2|3" channel="listing" limit="15"}
{if status == "1" orderby="random"}
   Entries here
{if:elseif status == "2" orderby="title" sort="asc"}
   Entries here
{if:else orderby="title" sort="asc"}
   Entries here
{/exp:channel:entries}
3
  • Are you okay with using Stash? Apr 16, 2013 at 21:31
  • Im open to it, can you tell me more about it and how it would apply in this situation? Apr 16, 2013 at 23:09
  • If that helped, could you consider marking any of the anwsers below as a correct answer ? Mar 11, 2014 at 17:31

2 Answers 2

2

If you don't mind using Stash, you could achieve it that way.

The idea is that you output all the channel entries once, and not worry about the order.
You 'stash' the appropriate fields inside a list, where you will retrieve it later.

{exp:stash:set_list name="my_listing" parse_tags="yes"}
  {exp:channel:entries status="1|2|3" ...}
    {stash:the_title}{title}{/stash:the_title}
    {stash:the_status}{status}{/stash:the_status}
  {/exp:channel:entries}
{/exp:stash:set_list}

Then later in your template, you would retrieve them in the appropriate order.

<ul>

{exp:stash:get_list name="my_listing" match="#1#" against="the_status" orderby="random"}
  <li>{title}</li>
{/exp:stash:get_list}

{exp:stash:get_list name="my_listing" match="#2#" against="the_status" orderby="the_title" sort="asc"}
  <li>{title}</li>
{/exp:stash:get_list}

{exp:stash:get_list name="my_listing" match="#3#" against="the_status" orderby="the_title" sort="asc"}
  <li>{title}</li>
{/exp:stash:get_list}

</ul>

The match and against parameters allow you to retrieve a filtered version of your listing. (match is a regular expression)

(I've prepended the stash variables with the_ so you don't confuse them with the channel entries variables. They could be whatever you want.)

4
  • Hey jason, seems we posted exactly at the same time ;o) Apr 17, 2013 at 13:34
  • I'll scissor/paper/rock you for the rep. Apr 17, 2013 at 17:14
  • rock. Voted you up ;o) Apr 19, 2013 at 9:21
  • See my note above... displays in the right order (and sort) just not all the records Apr 24, 2013 at 18:23
2

You could use Stash by Mark Croxton to accomplish that without multiple channel entries loops. Here is the kind of code I would use with the dev branch of Stash.

{exp:stash:set_list name="mylistname" parse_tags="yes"}
    {exp:channel:entries status="1|2|3" channel="listing" orderby="random" dynamic="no" limit="15"}
        {stash:st_title}{title}{/stash:st_title}
        {stash:st_status}{status}{/stash:st_status}
    {/exp:channel:entries}
{/exp:stash:set_list}

{!-- get status 1 entries randomly ordered --}
{exp:stash:get_list name="mylistname" match="#^1$#" against="st_status"}
    {st_title}
{/exp:stash:get_list}

{!-- get status 2 entries ordered by title asc --}
{exp:stash:get_list name="mylistname" match="#^2$#" against="st_status" orderby="st_title" sort="asc"}
    {st_title}
{/exp:stash:get_list}

{!-- get status 3 entries ordered by title asc --}
{exp:stash:get_list name="mylistname" match="#^3$#" against="st_status" orderby="st_title" sort="asc"}
    {st_title}
{/exp:stash:get_list}

That way you just use one channel entries loop to get all your entries in random order and build a stash list, then use multiple stash get_list to filter and order that list the way you want.

Hope it helps

EDIT see comment below.

You want a paginated list of all your 10000 entries, ordered by status (all entries from status one, then all from status 2, etc.) ... but you also want the entries from status 1 to be ordered randomly and the other two ordered by title.

(partial) solution 1: native tags

You can orderby status and then by title using native tags.

{exp:channel:entries status="1|2|3" channel="listing" orderby="status|title" sort="asc|asc" dynamic="no" limit="15"}
    {title}
{/exp:channel:entries}

The remaining problem is ordering the status 1 entries randomly.

Solution 2: custom plugin

You could also look at extending the channel:entries module. Here is an article detailing the process. Obviously you will need to adapt it to your needs.

5
  • We have implemented as you have suggested, and some of what we need is working however we seem to need additional instructions for display. Currently a set number of records displays (in the example above 15), however we get some from each status - not all 15 from status 1, then paginating the rest in order. Ideally, using pagination, we would have a number of pages of status 1, THEN pages of status 2 and so on. Currently the first page contains random numbers of each category (albeit sorted correctly) Ideas? Apr 24, 2013 at 18:19
  • FYI - we are dealing with 10,000+ records. When set_list runs, do we need to tell it to query ALL records in order to have it build the list with all the records (and their statuses) - instead of just the first 100? Apr 24, 2013 at 18:34
  • See If I get this right. You want all your 10000 entries, ordered by status (all entries from status one, then all from status 2, etc.) and paginated ... but you also want the entries from status 1 to be ordered randomy and the other two ordered by title ? Apr 24, 2013 at 19:56
  • Bingo! If it helps, the site is a business directory, and each business has the option of choosing a record layout (basic, enhanced and super). In the category list view, super are shown first (random order), then enhanced (alpha), then basic. Each category might only have 55 entries, but if we paginate at 15, we want all the supers shown first, then enhanced, then basic. Apr 24, 2013 at 20:24
  • Thought about it a bit more: see above Apr 25, 2013 at 11:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.