ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Limit to 12 entries by status, but show random entries if less than 12 with that status

June 21, 2012 3:46pm

Subscribe [2]
  • #1 / Jun 21, 2012 3:46pm

    Doug E

    98 posts

    The home page needs to show 12 entries (books for sale). They want to feature some books, maybe more than 12 at a time, maybe less. Featured books will always appear on the home page. If there are 15 books with a status of “featured” that is easy, limit to 12 and sort randomly. However if there are less than 12, say 10, with that status there will then only be 10 on the home page if I limit to 10 and only show the status of “featured”.

    Question: is it possible to round out the home page to 12 with non-featured random entries?  Some kind of php if statement that counts the “featured” entries displayed, then shows however many needed to add up to 12?

  • #2 / Jun 21, 2012 4:30pm

    John St-Amand

    865 posts

    I understand what you’re aiming for - giving preference to items with a “featured” status and backfilling when you don’t meet a given number (in this case 12).  Unfortunately since you can’t order by status, in a single entries loop you can’t even put only “featured” items at the top of a single entries loop - at least not natively.

    Instead, I might suggest “cheating” a bit by creating a custom field to act as a status. Then you could use that custom field as your orderby parameter and have a single limit parameter of “12” set.  So for example, you might have a custom field called “cf_book_status” that is a mandatory radio button with two options: featured and regular (as examples).  Then your entries loop you would have:

    {exp:channel:entries channel="book" orderby="cf_book_status" sort="asc" disable="member_data|pagination" limit="12" dynamic="no"}
    {your entry data here}
    {/exp:channel:entries}

    Because “f” comes before “r”, your order would put featured above regular “status” books.  And no matter how many entries there are with one “status” versus the other, you would always then have 12, with regular entries filling the gap if you have fewer than 12 set to “featured”.

    The downside to this approach, however, is that it does not allow you to randomize the entries.  If you need a combination of 12 entries of both statuses, with preference given to featured and backfilling of the entries in a single loop, i suspect you’d need a query for that - and that’s not my area.

    Hope this helps a little at least.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases