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

Query Loop: Allow custom "Order by" options #40170

Open
ryelle opened this issue Apr 7, 2022 · 9 comments
Open

Query Loop: Allow custom "Order by" options #40170

ryelle opened this issue Apr 7, 2022 · 9 comments
Labels
[Block] Query Loop Affects the Query Loop Block [Feature] Extensibility The ability to extend blocks or the editing experience [Type] Enhancement A suggestion for improvement.

Comments

@ryelle
Copy link
Contributor

ryelle commented Apr 7, 2022

I have a Query Loop using a custom post type (Sessions), and I want to order them by session date (not publish date). Session date is a meta value, though I've added a custom handling for orderby=session_date in the API — but I can't select this option in the default Query Loop settings, and I can't change the settings to add it (see WordPress/wordcamp.org#754 for my initial attempt at this).

A possible idea for this could be to allow filtering of the orderOptions here. Then plugin devs could add custom options, like { label: 'Earliest first', value: 'session_date/asc' }, trusting that the API will handle the orderby correctly.

@ryelle ryelle added [Feature] Extensibility The ability to extend blocks or the editing experience [Block] Query Loop Affects the Query Loop Block labels Apr 7, 2022
@joemaller
Copy link
Contributor

Being able to filter orderby input choices would be a great first step, but supporting more native ordering options would be welcome. One very-common option that's missing is menu_order which is frequently used to sort page-like custom post types and also by several ordering plugins.

@joemaller
Copy link
Contributor

Issue #24934 seems to be where this is tracking

@antonyjsmith
Copy link

I've had some major struggles with this recently and ending up having to remove the the Query Block completely from my beautiful FSE based project and replace it with a shortcode. The inability to modify or filter the block with complex queries to implement things like meta_query is very problematic and will inevitably lead to rather messy hybrid solutions like mine, which ultimately defeats the entire purpose of FSE.

@ntsekouras
Copy link
Contributor

@antonyjsmith I can understand folks need more flexibility and we always try to make things better, but all the help we can get is welcome.

Some of things are quite complex like the meta_query. Can you share the most important things you were missing and had to remove the block?

@antonyjsmith
Copy link

antonyjsmith commented Aug 16, 2022

I have a custom field - which happens to be a price - which tbh is a trivial query to build and I cannot order the query by that field [I had to come up with a work around just to display the custom field in the first place but that's another issue].

In reality pretty much anything beyond the most basic sorting options are off the table which creates me a lot of problems. Had menu_order been available I could possibly made do but that's still not implemented either.

This being said, I agree meta_query can be quite complicated and think any attempt to implement this via the block editor would be tricky and ultimately likely detrimental to the user experience. However not providing a hook or filter for the query loop feels like a major oversight - I think there is actually a pull request to implement this at the moment but it remains unmerged.

The issue here was a rerun of a frequent problem I have with Gutenberg. Run into a limitation > Check documentation [realise there either is none or it doesn't offer any developer details] > Move to Github > Read multiple tickets for hours that have often been open for years > eventually find a ticket related directly to my issue > discover there is no workaround or there's an unmerged pull > solve the issue using previous method [in this case shortcode] in a fraction of time than it took to realise the functionality doesn't exist.

The open issues on the Github repo is somewhat of a dumpster fire and tbh I've spent more time than I care to add up trawling through on futile workaround hunts.

I get that implementing features using the new paradigm takes a ton of time and effort but just leaving them out and not adding filters or hooks for developers is a major oversight.

@ryelle
Copy link
Contributor Author

ryelle commented Oct 13, 2022

There is now a filter for the query vars (query_loop_block_query_vars, documented here) which might help in some cases.

Unfortunately it doesn't solve the original problem, but it's been helpful for other Query Loop modifications, so I wanted to mention it on this thread.

@jamesmosier
Copy link

@slingshotdesign
Copy link

slingshotdesign commented Jan 27, 2023

There is a good article about how to make the Query Loop block orderby a custom field (meta_key) at the link below.

Currently it requires:

  1. Create a Query Loop block Variation
  2. Using the pre_render_block filter to make use of the query_loop_block_query_vars filter to render the front end
  3. Using the rest_{post-type}_query filter to to render the back end

NOTE: the code in the following article has an error. Change $query['orderby'] = 'meta_key'; to $query['orderby'] = 'meta_value';

https://wpfieldwork.com/modify-query-loop-block-to-filter-by-custom-field/#loop-modifications

@jordesign jordesign added the [Type] Enhancement A suggestion for improvement. label Sep 14, 2023
@francescobondi
Copy link

I’m trying to apply changes to the query for a query loop block, inspired by the pre_render_block filter above. Unfortunately, while the changes are being applied to the block that I want, they’re also being applied to all subsequent query loop blocks on the page. Given that the check within the filter is based on the passed-in $parsed_block (which contains data for the first block that matched the check within the pre_render_block-hooked function). What am I missing? Do you know how to help me?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Query Loop Affects the Query Loop Block [Feature] Extensibility The ability to extend blocks or the editing experience [Type] Enhancement A suggestion for improvement.
Projects
None yet
Development

No branches or pull requests

9 participants