Site logo

Archived topic

I want to show random post daily basis

7 replies · Started by Sabbir on November 8, 2022

Viewing posts 1–8 of 8

Hi.

Is there any way to change the query loop post-change daily basis?

I want to show this option in today’s daily prayer section.

Thanks

Hi Sabbir,

There isn't a way to do this through GenerateBlocks settings. What you can do however is to randomize it on each page load.

To do so, add cu-randomize-query to the class list of the Grid Block inside the Query Loop Block. Then, add this snippet:

add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) {
		if ( ! is_admin() && ! empty( $attributes['className'] ) && strpos( $attributes['className'], 'cu-randomize-query' ) !== false ) {
			
			$query_args['orderby'] = 'rand';
		}

		return $query_args;
	}, 10, 2 );

Adding PHP: https://docs.generatepress.com/article/adding-php/#code-snippets

It should only work for Query Loops Blocks that have the class cu-randomize-query.

Can you test the code above? I updated it.

With regards to transients, it works by caching the instance of a Query. To do so, you'll need to alter the code in the plugin itself.

An alternative you can do is to use a Caching plugin capable of Object caching. See here for more info about Object cache: https://wp-rocket.me/blog/object-caching-use-wordpress/#section-2:~:text=I%20Want%20This!-,So%20What%20is%20Object%20Caching%3F,-Object%20caching%20involves

It basically stores database queried results. Most Caching plugins have a Cache Lifespan, that is, the cache is refreshed after a certain time. WP Rocket for instance I believe does this. If so, you can set it through automatically refresh the cache every 24 hours. I'm not entirely sure if this will work though, but theoretically, it could.

I try the code, it's working now.

Is there any way to change it after 24 hours? The prayer changes randomly after refresh.

Or

I use siteground cache plugin, is there any guide to do this?

Hi there,

speak to your hosting company, explain that you want to cache that page for 24 hrs.
they should be able to assist with that.

Thanks David.

Let us know how you get on!

This archived topic is closed to new replies.