Random Sort and Caching

6 min read

Updated September 7, 2026

Setting a Gallery to sort at random asks a question most other settings do not: when should the order be decided? FotoGrids decides it on the server while building the page. Anything that stores that page — the FotoGrids cache, a page-builder cache, a caching plugin, a CDN — stores the order along with it, and every visitor who receives the stored copy sees the same sequence.

Randomize Order decides how FotoGrids answers that. The default asks the server for a fresh selection once the page has loaded, which no cache can freeze.

Location Gallery Settings Sorting Randomize Order


Why a cached page stops looking random #

A cache stores one rendered copy of a page and serves it to everyone until it expires. That is the whole point of a cache, and it is what makes a cached site fast. It also means whatever the server decided while building that copy is now fixed for the life of the entry — including which items were picked and what order they went in.

The effect is often mistaken for a bug. The order looks random the first time, because that request is the one that built the cached copy. Every visitor after that receives the same sequence. On a Single Item layout it reads as the same item appearing every time.

Several caches can be involved at once, and each holds its own copy:

CacheWhat it stores
FotoGrids cacheThe rendered Gallery HTML, in the database. On by default, switched per Gallery under PerformanceCaching.
Caching pluginThe whole page. WP Rocket, LiteSpeed Cache, W3 Total Cache, WP Super Cache and similar.
Host cacheThe whole page. Managed WordPress hosts usually run a server-level cache you do not configure yourself.
CDNA copy at the edge. Cloudflare, Bunny, Fastly and similar, often for far longer than the origin intended.
Page builderRendered widget output. Element or template caching in Elementor, Divi and others.

FotoGrids controls only the first of those, and it is on by default — so a Gallery can look frozen on a site running no caching plugin and no CDN at all. For every other layer FotoGrids can send a request not to store the page, which most caching plugins and hosts honor and some CDN configurations ignore. It cannot switch them off.


Order versus selection #

Two different things can vary, and the three modes differ in which ones they reach.

The order is the sequence the items appear in. The selection is which items get rendered at all. They are the same thing only when the Gallery renders every item it holds. Two common setups render a subset:

  • A paginated Gallery renders one page at a time. Display Mode defaults to Break into Pages at 12 items on desktop, so most Galleries render a subset by default.
  • The Single Item layout renders exactly one item, unless Animate Images is on.

On those, changing the order alone accomplishes nothing visible — reordering a single item is a no-op, and reshuffling the same 12 items still shows the same 12 items.


FotoGrids’ three randomization modes #

SettingRandomize Order
OptionsFully random each visit Reorder only Random on every request
DefaultFully random each visit
Visible whenDefault Sort Order is set to Random

Fully random each visit

The page is delivered from cache as usual. Once it loads, FotoGrids asks the server for a new random selection and swaps it in. Because the request happens after delivery, no cache upstream can affect it — both the selection and the order vary on every visit.

The items are held hidden while the request is in flight, so the visitor never sees one set of items replaced by another. If the response has not arrived within a short window, the server’s own selection is revealed instead and the late response is discarded — a frozen order is better than items changing under the visitor’s eye.

  • Works on paginated Galleries and on Single Item.
  • Every cache keeps working, so the page itself stays as fast as any other.
  • Costs one request per visit. It renders the Gallery only, not the whole page, so it is far cheaper than turning caching off.
  • Visitors with JavaScript turned off see the server’s selection.

Reorder only

FotoGrids rearranges the items the page was already delivered with. No request, nothing to wait for.

This changes the order but never the selection, so it is the right choice only when the Gallery already renders every item — Display Mode set to Show All Items, on a layout that shows more than one item.

On a paginated Gallery this mode shows the same items on every visit, rearranged. On a Single Item layout without Animate Images only one item is rendered, so it changes nothing at all. Use Fully random each visit for either.

Random on every request

FotoGrids chooses a new selection on every request and stops the response from being stored: the FotoGrids cache is bypassed for that Gallery, and the page is sent with headers asking caching plugins, hosts and CDNs not to keep it.

This is the only mode that varies without JavaScript, which matters when a downstream consumer reads the rendered markup rather than viewing the page. It is also the only mode that is not guaranteed: FotoGrids can ask the layers it does not control to skip the page, but a CDN configured to cache everything will store it anyway, and the order freezes again. Check a live page after switching.

Choosing Random on every request makes every page containing this Gallery uncacheable. Those pages will be rebuilt by WordPress on every visit, which is noticeably slower than serving a cached copy — particularly on shared hosting or a high-traffic page.


Which mode to choose #

Keep Fully random each visit unless you have a reason not to. It is the only mode that varies both the selection and the order on every hosting setup, and the request it costs is far cheaper than losing page caching.

Choose Reorder only when the Gallery shows all its items and you want to avoid the request entirely.

Choose Random on every request only when both of these are true: the order must vary for visitors without JavaScript, and you accept that the pages holding this Gallery will no longer be cached. If you already run a caching plugin or a CDN, verify with the header check below afterwards — FotoGrids asks those layers not to store the page, but some CDN configurations cache regardless.


How to tell whether your pages are cached #

Test while logged out. FotoGrids editors and preview requests always receive live output, so a Gallery that looks correctly random to you can still be frozen for everyone else.

The quick check

Open the page in a private or incognito window, reload it three or four times, and watch the items. If they never change, something is serving a stored copy.

Reading the response headers

Open your browser’s developer tools, switch to the Network tab, reload the page, click the first request in the list, and read the Response Headers. These are the ones worth looking for:

HeaderWhat it means
cf-cache-statusCloudflare. HIT means the page came from the edge.
x-cacheUsed by many CDNs and hosts. HIT means a stored copy.
ageSeconds since the stored copy was created. Any value above 0 means the response was cached.
x-litespeed-cacheLiteSpeed Cache. hit means a stored copy.
cache-controlAn s-maxage or max-age above a few seconds means the page is meant to be stored.

From the command line

Request the page twice and compare. The second response is the one that reveals a cache:

curl -sI "https://example.com/your-gallery-page/" | grep -iE "^(age|cache-control|cf-cache-status|x-cache|x-litespeed-cache):"

A page can be cached even when none of these headers appear. Server-level caches on managed hosts sometimes serve a stored copy without advertising it. If the items never change in a private window and you have ruled out the FotoGrids cache, assume something upstream is storing the page.


Overriding the behavior in code #

The fotogrids/cache/bypass_page_cache filter has the final say over whether a page carrying a random Gallery asks host, page-builder and CDN caches to skip it. It receives the value the Randomize Order setting resolved to, so returning it unchanged keeps the setting in charge.

It covers only the caches FotoGrids does not own. The FotoGrids render cache is governed separately by fotogrids/cache/should_cache, and a Gallery set to Random on every request skips that cache before the filter runs — so this filter cannot switch it back on.

// Never opt out of page caching, whatever the Gallery setting says.
add_filter( 'fotogrids/cache/bypass_page_cache', '__return_false' );
// Opt out only for one Gallery.
add_filter(
    'fotogrids/cache/bypass_page_cache',
    function ( $bypass, $settings, $gallery_id ) {
        return 42 === $gallery_id ? true : $bypass;
    },
    10,
    3
);

When the filter returns true, FotoGrids defines DONOTCACHEPAGE and sends Cache-Control: no-store for that request.

Was this article helpful?