Random only working when logged in

On this site, aeolidia.com, I’ve added a loop for the image in the hero section. It’s supposed to show a random ‘post’ and works perfectly when you’re logged in. But when you’re logged out, it just shows the most recent one.

Sounds like the loop is being cached but caching is disabled for logged in users. Does the post change when you clear the site cache?

@eliot any thoughts on how Jenn could get around this?

No, even when the cache is cleared the latest post always shows. Same for incognito.

In that case it’s possible that your host has disabled random orderby queries because they can cause performance issues. If that’s the case you can maybe change a setting to allow random orderby, or I have a workaround that would get around the performance issue and probably won’t be blocked by your host (WP Engine, right?).

<List randomPosts>
  <Loop type=post count=10>
    <Item><Field id /></Item>
  </Loop>
</List>

<Set randomInt><Math>rand_int(1, 10)</Math></Set>
<Set postID><Field list=randomPosts item="{Get randomInt}" /></Set>
<Loop type=post include="{Get postID}">
  <!-- your post item markup here -->
</Loop>
  1. We start by generating a list of recent post IDs. This can be as long as you like, but I’ve limited it to the 10 most recent posts.
  2. Then we create a variable with the math tag that is a random integer between 1 and 10 (change the upper limit if you change the count of the post loop inside the <List> tag)
  3. Then we create another variable that gets the list item at the index that matches the random variable. This final variable outputs the post ID stored in the item, so we can pass that to the post loop to get a random post from the set each time a user visits the page.

This might still get cached depending on your setup! Let me know if it works!!

From the issue description, it does sound like order by random is cached or disabled for logged-out users. I heard about it before, that some hosting services do this for performance reason. Thanks for the link, @julia - that article by WP Engine is informative and explains how to change the site setting.

A creative solution, to manually get a random item - I remember you found that function rand_int in the Math module, I didn’t know about before. Since then, a new tag called Random has been added - here’s the documentation page. It can simplify the code a little bit:

item="{Random from=1 to=10}"

About the result being cached, the WPE article says at the end:

Remember that the page generated by PHP will be cached for 10 minutes in our proprietary page caching system (Evercache). This means a randomized result will still only show every 10 minutes for your new users.

Here’s another attempt to get a random post without using orderby=random.

This one accounts for when there are less than 10 recent posts (or any max number to limit).

<Set maxCount>0</Set>
<List recentPosts>
  <Loop type=post count=10>
    <Item><Field id /></Item>
    <Set maxCount><Get loop=count /></Set>
  </Loop>
</List>

<Set randomNumber><Random from=1 to="{Get maxCount}"></Set>
<Set randomPostID><Field list=recentPosts item="{Get randomNumber}" /></Set>

<Loop type=post include="{Get randomPostID}">
  Random post: <Field title />
</Loop>
1 Like

Thanks for your help! Unfortunately, the host is WP Engine and their caching doesn’t allow this to work. Even with the setting changed and the revised code. It does reset every once in a while when the cache is cleared, but that’s about it.