Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Latest Posts] Add Order items by: random #23524

Closed
ghost opened this issue Jun 26, 2020 · 12 comments
Closed

[Latest Posts] Add Order items by: random #23524

ghost opened this issue Jun 26, 2020 · 12 comments
Labels
[Block] Latest Posts Affects the Latest Posts Block REST API Interaction Related to REST API [Type] Enhancement A suggestion for improvement.

Comments

@ghost
Copy link

ghost commented Jun 26, 2020

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

@ntsekouras
Copy link
Contributor

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

@ntsekouras ntsekouras self-assigned this Jun 29, 2020
@ntsekouras ntsekouras added [Block] Latest Posts Affects the Latest Posts Block [Type] Enhancement A suggestion for improvement. REST API Interaction Related to REST API labels Jun 29, 2020
@ntsekouras
Copy link
Contributor

@ntsekouras ntsekouras removed their assignment Jun 29, 2020
@mcsf
Copy link
Contributor

mcsf commented Jun 29, 2020

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?

@TimothyBJacobs
Copy link
Member

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.

@ntsekouras
Copy link
Contributor

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

@coreyworrell
Copy link
Contributor

coreyworrell commented Apr 30, 2021

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.

@ntsekouras
Copy link
Contributor

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.

@aristath
Copy link
Member

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.

@coreyworrell
Copy link
Contributor

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

@aristath
Copy link
Member

aristath commented May 5, 2021

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

@TimothyBJacobs
Copy link
Member

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.

@jbreitenbucher
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Latest Posts Affects the Latest Posts Block REST API Interaction Related to REST API [Type] Enhancement A suggestion for improvement.
Projects
None yet
Development

No branches or pull requests

6 participants