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.

show one weblog entry randomly out of subset of entries

November 30, 2010 11:10pm

Subscribe [7]
  • #1 / Nov 30, 2010 11:10pm

    SteveSteveSteve

    12 posts

    I’m working on a client site that was originally setup by someone else.  I’m still somewhat new to EE.

    The site has a weblog of content called “markers”.  The client would like me to be able to show one randomly selected marker on the home page.  That’s easy to do with orderby=“random” and limit=“1”, however, not every entry from this weblog should be available to be shown on the homepage.

    How should I go about setting this up so I can have the template pick out just one entry of a subset of entries in a weblog?

    Your assistance is appreciated….

    - Steve

  • #2 / Dec 01, 2010 11:47am

    Sue Crocker

    26054 posts

    Hi, Steve. One way to do this would be to use a custom status. Just be sure if you go that route to check for the other status in regular exp:weblog:entries loops.

  • #3 / Dec 23, 2010 9:43pm

    SteveSteveSteve

    12 posts

    Hi, Steve. One way to do this would be to use a custom status. Just be sure if you go that route to check for the other status in regular exp:weblog:entries loops.

    Well… I’ve now tried this two ways.  So far neither way gets me the results I want.

    My first attempt I created a drop-down field called “show_on_home_page”.  If “show_on_home_page==‘yes’” then, print out the fields I want from that entry.

    I got two not-quite-right results from this try.  The first one I used this code:

    {exp:weblog:entries weblog="plaques" orderby="random" limit="1"}
    {if show_on_home_page == "yes"}
    <b>title: {title}</b>
    
    <hr>
    {if:else}
     we got no entries
    {/if}
    {/exp:weblog:entries}

    Looks like it takes a single entry out of the weblog - if it’s not a “show on homepage” entry then we get nothing to show.

    Second incorrect attempt with “show_on_home_page” uses this code:

    {exp:weblog:entries weblog="plaques"}
    {if show_on_home_page == "yes"}
    <b>title: {title}</b>
    
    {/if}
    {/exp:weblog:entries}

    I would think this should pull up *all* of the entries where ‘how_on_home_page == “yes”’, but it only pulls up two of them.

    gnarrr!

    So, Take Two I created a custom status called “home-ok”,  Now this code:

    {exp:weblog:entries weblog="plaques" status="home-ok" orderby="random" limit="1"}
    <b>title: {title}</b>
    
    {/exp:weblog:entries}

    Pulls up one of the three entries marked with this status… But it’s the same one every time.

    To recap: this weblog called “plaques” has a whole bunch of entries.  I want to be able to mark a subset of these entries in such a way that I can pull just one entry that’s been marked and show it’s fields on the home page.  Not every entry is something to be shown on the home page… I also want it to pull a random one of that subset every time.

    Surely I’m being dim and just not getting something.  Help?

    You can see this stuff not working here: http://www.kcymaerxthaere.com/testhome

  • #4 / Dec 27, 2010 3:03pm

    Brandon Jones

    5500 posts

    SteveSteveSteve,

    1. What version and build of EE are you using?
    2. Do you have any caching enabled? Specifically, Dynamic Channel Query Caching? Try disabling.
    3. Make sure to clear your EE caches after making any changes in #2.

  • #5 / Dec 30, 2010 2:20pm

    SteveSteveSteve

    12 posts

    SteveSteveSteve,

    1. What version and build of EE are you using?
    2. Do you have any caching enabled? Specifically, Dynamic Channel Query Caching? Try disabling.
    3. Make sure to clear your EE caches after making any changes in #2.

    * We are running 1.6.8 Build:  20090723

    * Dynamic caching was on, I just turned it to off.

    * Cache files deleted…

    I’m still getting the same results here: http://www.kcymaerxthaere.com/testhome

  • #6 / Dec 30, 2010 4:30pm

    Ingmar

    29245 posts

    The custom status one seems to be working fine, I get a new one for every page load. An update to EE 1.7 is strongly recommended, by the way.

  • #7 / Dec 30, 2010 6:29pm

    SteveSteveSteve

    12 posts

    The custom status one seems to be working fine, I get a new one for every page load. An update to EE 1.7 is strongly recommended, by the way.

    Right you are!  The status method is pulling a random entry just like I want.  Yay!

    Now, if I understand what’s happening in this code segment correctly -

    {exp:weblog:entries weblog="plaques" orderby="random" limit="1"}
    {if show_on_home_page == "yes"}
    <b>title: {title}</b>
    
    {pl_homepage_blurb}
    
    {pl_comments}
    {/exp:weblog:entries}

    One entry is pulled.  if show_on_home_page is set to “yes” then it’s displayed.  If not, the moves on to the next statement.

    How would one loop through that until a single entry that meets the criteria is found?

    meanwhile… I see about upgrading to 1.7.

  • #8 / Dec 31, 2010 4:11am

    John Henry Donovan

    12339 posts

    SteveSteveSteve,

    One entry is pulled.  if show_on_home_page is set to “yes” then it’s displayed.  If not, the moves on to the next statement.

    One entry is pulled and tested against that conditional but it stops there as there is a limit of one. So having that condition met is random also.

    But regardless you are up and running with the status method now?

  • #9 / Dec 31, 2010 7:10pm

    SteveSteveSteve

    12 posts

    SteveSteveSteve,

    One entry is pulled.  if show_on_home_page is set to “yes” then it’s displayed.  If not, the moves on to the next statement.

    One entry is pulled and tested against that conditional but it stops there as there is a limit of one. So having that condition met is random also.

    But regardless you are up and running with the status method now?

    I haven’t implemented the random-by-status method yet because mastering the show_on_homepage==“yes” method would be more powerful. 

    Is there any way to keep looping through a weblog until you get one entry and then stop?

    —S!

  • #10 / Jan 02, 2011 7:40am

    Greg Salt

    3988 posts

    Hi SteveSteveSteve,

    If you know that only one entry will satisfy your conditional then just remove the limit parameter from your weblog entries tag. This is not the most efficient way to do this though - filtering your entries using custom statuses and/or categories is much more powerful and will reduce the number of SQL queries that are required.

    Cheers

    Greg

  • #11 / Jan 06, 2011 8:37pm

    SteveSteveSteve

    12 posts

    Hi SteveSteveSteve,
    If you know that only one entry will satisfy your conditional then just remove the limit parameter from your weblog entries tag. This is not the most efficient way to do this though - filtering your entries using custom statuses and/or categories is much more powerful and will reduce the number of SQL queries that are required.

    It would be mighty silly to ask about how to pull a random result from a set with only one possible result, wouldn’t it?

    From the beginning.  Here’s the client site: http://www.kcymaerxthaere.com/

    The client has an existing weblog to hold the data on the “markers”.  We want to randomly display a marker in that middle column where it’s currently hard-coded in.

    The catch is not every marker is appropriate to show in that column.  The client wants to show a random marker, but somehow make sure it’s one that’s approved to be shown in that homepage feature.

    We’ve figured out a way to do that using “status”, but it seems to me that doing this in an {exp:entries} loop would be more powerful.

    Or am I just a confused puppy?

  • #12 / Jan 06, 2011 9:41pm

    johndwells

    94 posts

    Hi Steve,

    Don’t give up, you’re close! I think what you need is this:

    http://expressionengine.com/legacy_docs/modules/weblog/parameters.html#par_search

    It’s a search=’’ parameter that you can pass into {exp:weblog:entries}. Try this:

    1. Modify your ‘show_on_home_page’ custom field so that it is searchable

    2. Modify your opening {exp:weblog:entries} tag to:

    {exp:weblog:entries
         search:show_on_home_page="yes"
         dynamic="off"
         weblog="plaques"
         orderby="random"
         limit="1"}

    (p.s. I added dynamic=“off” just in case you attempt to use this code on a “single entry” page, in which case you might get borked results)

    3. You can then remove your {if} conditional, as it should already be satisfied.

    Let us know how you get on…

    Cheers,
    John

  • #13 / Jan 06, 2011 9:44pm

    johndwells

    94 posts

    p.s.  FYI in the EE docs it says that for the search:= parameter:

    “NOTE: Only custom fields of the type “Text Input”, “Textarea”, and “Drop-down Lists” are searched with this parameter.”

    So you might need to abide by that too…

    Cheers,
    John

  • #14 / Jan 07, 2011 11:14am

    Ingmar

    29245 posts

    Thanks John. Does that work for you, Steve? If you need further assistance I’m happy to move this over to the CodeShare Corner for you, too.

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

ExpressionEngine News!

#eecms, #events, #releases