gutenberg icon indicating copy to clipboard operation
gutenberg copied to clipboard

Add Random to Order by options in Query Loop Block

Open jbreitenbucher opened this issue 4 years ago • 53 comments

What problem does this address?

The ability to have dynamic content on a page/post/site template. In current themes I often use a widget that returns random posts from a category to create a dynamic front page.

What is your proposed solution?

The query loop supports the ability to return random posts and I would suggest adding Random to the Order by options for the Query Loop Block.

jbreitenbucher avatar Apr 20 '22 14:04 jbreitenbucher

I just wanted to note that random queries are slow by default. So it might not be good to offer this option without pointing out the risks.

Mamaduka avatar Apr 20 '22 19:04 Mamaduka

I'm wondering why the method used by several Widgets like the Genesis - Custom Featured Post widget can't be used in the Query Loop Block? cfpw

jbreitenbucher avatar Apr 21 '22 12:04 jbreitenbucher

Related issue mentioning the performance issues, too: #24934

priethor avatar May 27 '22 12:05 priethor

Any update ?

Ayfri avatar Jun 08 '22 08:06 Ayfri

+1

In general, blocks should provide at least the same functionality that was available in widgets, and then some, rather than taking features away.

galbaras avatar Jul 06 '22 03:07 galbaras

+1

Would be great to have all the options we had in WP_Query.

VladNov avatar Jul 21 '22 15:07 VladNov

+1 I agree this should be added. Random does slow things down but is needed for a variety of purposes we run across and I find a need for it frequently. I also need it on a per-case/per-block basis (or per-widget) and not globally. An option where the other order options are seems to be the logical place.

stinkykong avatar Sep 08 '22 10:09 stinkykong

+1

cdils avatar Sep 26 '22 17:09 cdils

The note around it being slow appears applicable when the dataset is large, so perhaps it can either be solved in a similar way to this example: https://github.com/Automattic/vip-go-mu-plugins/blob/ba5fcff8bf5ca5b717675cf75fcb427aab77b5d4/vip-helpers/vip-utils.php#L744-L780.

pkevan avatar Nov 09 '22 17:11 pkevan

Noting for posterity's sake that some hosts, such as WP Engine disable 'orderby' => 'rand' by default unless a site admin explicitly allows it, which could lead to unexpected results for users that aren't aware.

A workaround solution such as the one @pkevan points to looks like a great alternative.

cdils avatar Nov 09 '22 18:11 cdils

WP_Query already includes rand as an orderBy option. So another alternative, if you want to create a random Query Loop right now, could be to edit the orderBy parameter of the Query Loop block in Gutenberg Code Editor, so it looks like this:

<!-- wp:query {"queryId":5,"query":{"perPage":3,"pages":0,"offset":0,"postType":"post","order":"desc","orderBy":"rand","author":"","search":"","exclude":[],"sticky":"","inherit":false}} -->

With this approach, it would work properly on the site, but you won't see the posts on the Editor side. If you want to make it work in the editor as well, you can always add the rand option in the REST API using the rest_this-post_type_collection_params filter as suggested here. With something like this:

function add_rand_orderby_rest_api( $query_params ) {
	$query_params['orderby']['enum'][] = 'rand';
	return $query_params;
}
add_filter( 'rest_post_collection_params', 'add_rand_orderby_rest_api' );

SantosGuillamot avatar Nov 09 '22 19:11 SantosGuillamot

The main point here is consistency and function, not performance.

If rand is disabled, it can't be used anywhere in WordPress, so that's consistent. If it's slow, the person using the widget can decide to use something else. But the option should be there, in the UI, without resorting to code editing, because WordPress caters for it.

Resolving the performance issue is a matter for the core WordPress team via Trac discussions. The Gutenberg team shouldn't hide options that are native to WordPress.

Team, please add the rand sorting option to the Query Loop block. One small step for you, one giant leap in consistency for users ;)

galbaras avatar Nov 10 '22 00:11 galbaras

+1

natirivero avatar Nov 14 '22 21:11 natirivero

@SantosGuillamot I've tried both of your suggestions, and neither of them works. Looking through the Gutenberg code, it's likely that this is because the JS code doesn't know what to do with the new option.

Not sure why #23524 was closed. This was a valid request from over 2 years ago, which should be implemented soon. Plugins should not make decisions (performance or otherwise) for site builders/owners, and should not disable options that are available in core for good reason.

galbaras avatar Dec 09 '22 02:12 galbaras

+1 - Maybe add ability to overlay txt onto image... But random would be great.

Hebhansen avatar Dec 14 '22 19:12 Hebhansen

+1

tnchuntic avatar Dec 19 '22 23:12 tnchuntic

Ad mentioned above, a lot of hosts (included but not limited to WPEngine) disable random database queries due to performance costs. I don't think that Core should provide a UI to do something that we already know is bad, and unsupported by a number of hosts. IMO, if a user wants to do something that is clearly not recommended, they can install a 3rd-party plugin. Core should not encourage "bad" queries.

aristath avatar Dec 20 '22 11:12 aristath

It's not for you to decide what's good or bad. If a host disabled this option, it won't work, but if it's available, it can be used. Similarly, if the site owner thinks it slows down the site, they can use something else, but at least they have a choice.

WordPress is a COMMUNITY project and you're ignoring the community here.

In my particular case, sorting testimonials by some other method gives worse user experience than if the page takes another 0.2s to load, so I prefer random sorting. I'm sure others have valid reasons too.

Also, the block editor is based on WordPress core, and unless the core team removes rand sorting, the editor should make it available in the UI and scripts. Nobody should ever need yet another plugin just to make use of something that's already there.

galbaras avatar Dec 20 '22 22:12 galbaras

@galbaras Let's keep it civilized. @aristath makes a good point and is not deciding or ignoring anything, just adding to the conversation.

I agree that core should encourage good performance and safety, cause with the community in mind, many of the people in it won't know what's happening behind the scenes and aren't aware of certain "risks". Core discourages bad practices in many ways already, taking away options from the community for their own safety and benefit.

There's also nothing wrong with using plugins, that's one of the biggest aspects that make WordPress so great and modular. Never needing a plugin for anything, but adding everything to core, is an ideology with a lot of bad consequences.

ghost avatar Feb 14 '23 15:02 ghost

Core discourages bad practices in many ways already, taking away options from the community for their own safety and benefit.

Well, I disagree with that, too. Both the core team and the Gutenberg team should accept they don't know the circumstances of every site and stop dictating things. Taking away a core option is unacceptable, no matter the reasoning.

There's also nothing wrong with using plugins

I agree, but plugins shouldn't be used to re-enable things disabled by other plugins. That's just ridiculous. It's much better if Gutenberg didn't disable rand sorting and nobody needed a plugin to re-enable it.

I have enormous respect for @aristath, but in this case, the Gutenberg team is dictating things to people who clearly resent this dictation and want something different. It's easy enough to have a setting for this, and have it turned off by default. An ever safer way, and just as easy, is to have filter for it. Either way, those who know what they're doing can enable it, and Gutenberg can support it, instead of dictating.

galbaras avatar Feb 14 '23 23:02 galbaras