Is there a way to set the items in a loop to display randomly? I’ve tried order=random and sort_order=random… neither of those worked. I’ve seen a couple people mention random sorting, but can’t find it in the documentation.
Is this for a post loop? If so, the docs suggest that you could use the query parameter orderby=random
. Seems like several of the other loop types also use the orderby
parameter. Hope that’s helpful!
It’s a taxonomy loop. This is the code I currently have on the site, orderby=random is sorting them alphabetically.
<Loop type=taxonomy_term taxonomy=project_type post=current count=3 orderby=random>
<div class="related">
<a href="{Field url}">
<div class="fl-post-image"><Field acf_image=cat_image size="small_list" /></div>
<h3 class="white"><Field title /> Projects</h3>
</a>
</div>
</Loop>
Hmm, looking at the docs for taxonomy term loops, it shows that the orderby
parameter accepts the following strings: “name, title (default), term_id, menu_order, count.” No mention of “random” being an option for taxonomy term loops for some reason, which seems potentially like a small oversight for that particular parameter.
@eliot if my understanding above is correct, can we allow taxonomy term loops to accept orderby=random
in a future update?
Thank you for pointing this out, Ben, I found the answer in the WordPress documentation for WP_Term_Query
, which the taxonomy term loop uses. In the parameter list, orderby “random” is missing/unsupported.
But I can see it would be useful to add support for it. In fact, it seems logical for all loop types to support it, as expected, instead of just the post loop.
By its nature, random order is not an efficient query: it will need to get all taxonomy terms (matching certain criteria) first, then order items randomly. I think that’s acceptable in most cases, unless there are thousands of terms.
OK, I’ve made a note to implement this, and will comment back here when it’s ready.
Oh that’s interesting that WordPress doesn’t even natively support randomly ordered taxonomy term loops.
Separate matter but related, orderby=random
does work for a loop that includes taxonomy like this:
<Loop type="quotes" taxonomy=quote_category terms="Front" count=8 orderby=random>
In case that helps anyone.