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.

Pulling a random entry?

April 04, 2011 1:24pm

Subscribe [2]
  • #1 / Apr 04, 2011 1:24pm

    Collin H

    18 posts

    I’ve browsed the available variables and parameters, but unless I’m overlooking the obvious I can’t find a way to do what I’m trying to do.

    Is there a “random” function in EE? I want to pull a random entry from a certain channel and display it in a particular spot on the site’s home page. Each time the home page loads, it pulls a new and random entry.

    Seems simple enough, but how?

  • #2 / Apr 04, 2011 1:31pm

    Mark Bowen

    12637 posts

    Hi Collin,

    Take a look at the orderby=“random” parameter for the Channel entries tag along with the limit=“1” parameter too.

    You’ll definitely want to make good use of the disable=”“ parameter too if you’re just going to be bringing back one entry.

    {exp:channel:entries
        channel="default_site"
        limit="1"
        dynamic="no"
        orderby="random"
        sort="asc"
        disable="member_data|pagination|categories"}
    
    {title}
    
    {/exp:channel:entries}

    Hope that helps a bit.

    Best wishes,

    Mark

  • #3 / Apr 04, 2011 1:35pm

    Collin H

    18 posts

    As I suspected, it was right there in plain sight! I’m new to EE and was aware of the “orderby” parameter but apparently glanced right over the “random” option.

    All else makes perfect sense as well. Thanks very much!

  • #4 / May 27, 2011 2:54pm

    Hello, I have another question related to random order. I have numerous entries with a few fields in them. One of the fields is a dropdown set to Yes or No that users can select.

    Here is what I am trying to accomplish.  We want those entries to display randomly every time a person visits that page (we’ve set limit to 1). We were able to figure that out no problem. The issue is when we want to temporarily stop the random so that only one of those entries displays for everyone and there is no random rotation.

    Here is what my code looks like now. I was trying to use conditionals but I am not sure how to make this work.

    {exp:channel:entries channel="sheriffs" dynamic="no" orderby="random search:featured_content="not IS_EMPTY" search:extended_featured_content="No" limit="1"}
    {featured_content}
    {/exp:channel:entries}

    So the logic behind this is 1)find the featured_content fields that are NOT empty and 2) have the extended_featured_content field set to “No”, 3) and display them randomly.

    However, let’s say we want one entry to be “extended” in other words to “turn off” the random loading of entries and ONLY show the one with the extended_featured_content set to “YES” we get problems.

    Does this make sense? Can you help?

    Thank you.

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

ExpressionEngine News!

#eecms, #events, #releases