Site logo

Archived topic

Questions about query loop

4 replies · Started by Kees on September 30, 2022

Viewing posts 1–5 of 5

Hi,

I have to questions. We have blogs and we show them on the homepage with a query loop.

1. Is it possible to show the post on the homepage with a queryloop in a random order in WordPress?
2. Is it possible to show the post ont the homepage with a queryloop in order of most read.

Would help a lot. Please let me know.

Regards,
Kees

Hi Kees,

Yes, it should be possible to add a GB Query Loop Block that's ordered randomly. Since your homepage is a static page, you can add it directly to the page. You'll need a code like this:

add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) {
    if ( ! empty( $attributes['className'] ) && strpos( $attributes['className'], 'my-class-name' ) !== false ) {
		$query_args['orderby'] = 'rand';
       }
	   return $query_args;
    }

    return $query_args;
}, 10, 2 );

Add my-class-name to the class list of the Grid Block in the Query Loop Block.

2. It should also be possible but this is more complicated. You would need a plugin that records post views, and then use the post meta for the views generated by that plugin in a custom code.

Lastly, for GenerateBlocks related inquiries, please reach out to us here: https://community.generateblocks.com/

Hi,

Thanks! Helped a lot!

There was an error in your code btw. But fixed it.

I see. You're welcome Kees!

This archived topic is closed to new replies.