Query Loop: Random Selection Of Posts

[ad_1]

After further troubleshooting…

Within your documentation, you mention that the Query Loop block makes use of the standard WordPress WP_Query() function, which does have the ability to return a random set of posts: 'orderby' => 'rand', according to here, but the UI does not have this option.

Is it possible to modify the Query Loop query outside of the UI or maybe add this option to the UI?

Plugin Support
David

(@diggeddy)

Hi there,

its an option in GenerateBlocks Pro, it accompanies the “Current Post” related args.

But you can use the generateblocks_query_loop_args hook to merge your own args if you want:

add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) {
    // give the grid block inside the query loop a class name of
    $myClass="my-class-name";
    if (
        ! empty( $attributes['className'] ) &&
        strpos( $attributes['className'], $myClass ) !== false &&
    ) {
    // Merge the current $query_args with editor defined args
    return array_merge( $query_args, array(
        'orderby' => 'rand',
    ) );
    }

    return $query_args;
}, 10, 2 );

Note: the rand process can be a server performance hog and some hosts disable it for the amount of staring it places on a database query. So you may need to check with the host if it doesn’t work.

There was stray ‘&&’ in there but this works perfectly!! 🤩

Thank you so much!

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer