• Resolved David Adams

    (@tictag)


    I am trying to create a grid of randomly selected posts from a pool of such posts, so that every time the page is rendered, a new grid is rendered.

    Is this possible with, for example, Query Loop?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter David Adams

    (@tictag)

    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.

    Thread Starter David Adams

    (@tictag)

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

    Thank you so much!

    Plugin Support David

    (@diggeddy)

    Ah, good spot.
    Glad to hear that worked.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Query Loop: Random Selection Of Posts’ is closed to new replies.