29th Aug '24
/
1 comment

x Number of Random Bricks Query Loop Items(s) to be Shown Every x Hours

A user asks:

Query a metabox group, but limit items?

...i cannot see how to pick a random text field in that group and change it every X minutes. is it doable?

i need to show only one item in the metabox group, at random, and randomly pick another after one hour.

btw, dunno why the groups have no options like posts or pages.

Non standard query types like ACF Repeaters, Meta Box Groups and Relationships do not have the same controls that the post query loops have.

This Pro tutorial shows how PHP transients and bricks/query/result filter can be used to specify how many random items should be output in the query loop's result set and for how long.

After implementing the tutorial, you could, for example, show one random row of a Meta Box group on page load, continue to show the same for an hour (the same for all visitors/users), and then output another random row (other than what was shown in the last hour) for the second hour and so on.

Before:

After:

After another hour:

Step 1

In a Bricks template or Page, set up your query loop.

In this example, we are querying a Meta Box group.

Step 2

Before we add the code snippet let's first understand what the original query result and the query object looks like.

Original query result:

This can be seen by adding this snippet in a code snippets plugin like WPCodeBox or in the child theme's functions.php at the end w/o the opening PHP tag:

This is a BricksLabs Pro tutorial.

For full access login below or create your BricksLabs Pro account

Get access to all 633 Bricks code tutorials with BricksLabs Pro

Related Tutorials..

Pro
“Truncate text to these many characters” Bricks Control

“Truncate text to these many characters” Bricks Control

Bricks provides a :<number> dynamic data tag modifier that can be used to limit the amount of text by the specified number of words. Ex.:…
Pro
Inserting Random Ad Between Posts in Bricks

Inserting Random Ad Between Posts in Bricks

How to insert random ads (post type) in between regular posts on the blog page.
Categories:
Pro
Ordering Meta Box group rows by a sub field in Bricks

Ordering Meta Box group rows by a sub field in Bricks

In the BricksLabs Facebook group a user asks: Any idea how to set the sort order of a cloneable group in the query? In this…
Filtering out Media Items from “Select post/page” Bricks control

Filtering out Media Items from “Select post/page” Bricks control

The dropdown that appears after choosing "Internal post/page" when setting a link in Bricks shows media (attachment) items besides Pages, Posts and other CPTs. If…
Pro
Filtering Meta Box Cloneable Group by Select Subfield for Multiple Bricks Query Loops with Conditional Output

Filtering Meta Box Cloneable Group by Select Subfield for Multiple Bricks Query Loops with Conditional Output

In the Bricks Facebook group a user asks: Consider this cloneable Meta Box field group for a Custom Post Type called Tour: with the Tour…
Pro
Programmatically populating ACF field values in WordPress

Programmatically populating ACF field values in WordPress

An example of how to set the values of a Select-type field with the names and labels of all public post types.
Categories: