Site logo

[Support request] I want to show random post daily basis

Home Forums Support [Support request] I want to show random post daily basis

Home Forums Support I want to show random post daily basis

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #2407406
    Sabbir

    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

    #2407429
    Fernando
    Customer Support

    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

    #2407461
    Sabbir

    Hi,

    Thank you!

    Is it work for one block/section or the whole site?

    I just want to do it for 1 section, which is the daily prayers section.

    Also, I can’t try the code, here is the error: https://prnt.sc/j26WyWEGXVLA

    Also, check if it helps: https://wordpress.stackexchange.com/questions/199619/random-post-once-per-day

    #2407558
    Fernando
    Customer Support

    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.

    #2407811
    Sabbir

    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?

    #2407980
    David
    Staff
    Customer Support

    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.

    #2408521
    Sabbir

    Thanks David.

    #2408524
    David
    Staff
    Customer Support

    Let us know how you get on!

Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.