gutenberg icon indicating copy to clipboard operation
gutenberg copied to clipboard

[Latest Posts] Add Order items by: random

Open ghost opened this issue 6 years ago • 12 comments

Hello! 🙏😊 Currently we can:

  • filter Posts by: author, multi-categories;
  • sort/order Posts by: Newest to oldest, Oldest to newest, A → Z, Z → A.

It would be good to have the ability to sort/order Posts randomly on the Latest Posts block. Currently, ordering is dualistic, which always disregards some posts in favor of others. When post dates or post titles are not relevant for establishing the order of equally important posts, the randomic sorting/ordering is necessary.

It improves the following scenarios: Category with important, timeless posts. Related Posts. Testimonial Posts.

I offer a mockup of the proposed solution:

image

ghost avatar Jun 26 '20 18:06 ghost

Hi @marceloaof ! That's a nice idea to implement to Latest Posts.

ntsekouras avatar Jun 29 '20 07:06 ntsekouras

Needs to change here: https://github.com/WordPress/WordPress/blob/71244fc29e1ab7117383f17bab8f47db91814087/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php#L2641

and add rand in enum in REST API in core.

ntsekouras avatar Jun 29 '20 11:06 ntsekouras

Needs to change here: https://github.com/WordPress/WordPress/blob/71244fc29e1ab7117383f17bab8f47db91814087/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php#L2641

and add rand in enum in REST API in core.

It's odd that WP_REST_Posts_Controller's param collection doesn't honour WP_Query's parameters. @marceloaof, this might be something to ask the REST API team — is it oversight, is it an intentional omission?

mcsf avatar Jun 29 '20 11:06 mcsf

rand was intentionally not supported, https://core.trac.wordpress.org/ticket/38693. It can be pretty brutal for performance, so I'd consider omitting it as a UI option.

TimothyBJacobs avatar Jun 30 '20 14:06 TimothyBJacobs

I'll close this issue for now due to the performance issues that rand can cause, as explained from @TimothyBJacobs.

ntsekouras avatar Jul 01 '20 07:07 ntsekouras

What would you recommend using instead of rand? I don't get why you would remove this option from Gutenberg instead of fixing the performance issue within WordPress itself, or at least letting each theme/site decide for itself if the performance hit is worth it or not. For example, we use a cache plugin, so the random query would only be happening during edit, and once every 15 minutes when the cache is cleared. We have used rand WP_Querys pretty often and never had any noticeable issues. The latest post block is perfect and does 99% of what we need already. Seems silly to create a plugin or custom block/template to recreate it all just for that.

It wouldn't even be a problem if there were way to even just filter the query that happens. Then we could just set the orderby in a filter, and even exclude the current post, etc. But as it is, I don't see any way to filter the query for this block.

coreyworrell avatar Apr 30 '21 00:04 coreyworrell

I don't get why you would remove this option from Gutenberg instead of fixing the performance issue within WordPress itself,

This is not a query that is WP specific but is related to most databases and without using a cache mechanism and with a possible large number of posts this can be indeed an intensive operation. IMO many users do not have the technical knowledge of such an impact and/or what even cache is..

Having said that, as you can see from @mcsf and my above comments I agree with you that WP_Query's parameters should be supported and I'd really love if you could raise that again in the existing trac ticket: https://core.trac.wordpress.org/ticket/38693 or even create a new one to be discussed again.

ntsekouras avatar Apr 30 '21 08:04 ntsekouras

As mentioned above, this is not a WordPress issue but a database/MySQL issue. Worth noting: A lot of hosts disable the ability to run rand queries by default (example). With that in mind, we can't include an option to order posts by rand, as that would give the impression to users that their site is broken.

aristath avatar Apr 30 '21 08:04 aristath

Allow it to be filterable then? add_filter('latest_posts_block_allow_rand', fn (): bool => true) or whatever.

coreyworrell avatar May 03 '21 22:05 coreyworrell

It would be safer to just build this as a separate block in a plugin if your site needs random order. Adding rand order in WordPress-core would introduce a lot of issues, so doing it in a plugin would be the safest and most future-proof way

aristath avatar May 05 '21 09:05 aristath

This is already filterable using rest_{$post_type}_collection_params. Gutenberg really should be reading from the schema for the post type and including/excluding those order by options based on the enum value.

TimothyBJacobs avatar May 05 '21 16:05 TimothyBJacobs

Probably naive, but why can't the Query Loop Block implement the method that is currently being used in a number of widgets such as the Genesis - Custom Featured Posts widget that allow your to order by random and a number of other things? cfpw

jbreitenbucher avatar Apr 21 '22 12:04 jbreitenbucher