- This topic has 28 replies, 3 voices, and was last updated 3 years, 2 months ago by
Alessandro.
-
AuthorPosts
-
August 3, 2022 at 10:56 am #2302320
Alessandro
Hi, I added a custom post ordering method (Menu Order) for a posts query:
add_action( 'admin_init', function() { add_post_type_support( 'post', 'page-attributes' ); } ); add_action( 'pre_get_posts', function( $query ) { if ( ! is_admin() && $query->is_main_query() && ( $query->is_home() || $query->is_archive() ) ) { $query->set( 'orderby', 'menu_order' ); $query->set( 'order', 'ASC' ); } } );
This worked as planned, but now I can no longer view or edit the styling of the query in the backend. It only shows “No results found.” and the browser inspector shows this error:
{"code":"rest_invalid_param","message":"Invalid parameter(s): orderby","data":{"status":400,"params":{"orderby":"orderby is not one of author, date, id, include, modified, parent, relevance, slug, include_slugs, and title."},"details":{"orderby":{"code":"rest_not_in_enum","message":"orderby is not one of author, date, id, include, modified, parent, relevance, slug, include_slugs, and title.","data":null}}}}
August 3, 2022 at 11:06 am #2302330Ying
StaffCustomer SupportHi there,
but now I can no longer view or edit the styling of the query in the backend
Do you mean a query loop block?
August 3, 2022 at 11:07 am #2302334Alessandro
Sorry that was ambiguous – that’s right. The query loop block still works (it displays the posts in the front end of the site) but in Gutenberg all I see is “No results found.”
August 3, 2022 at 11:15 am #2302346Ying
StaffCustomer SupportThat’s weird, the code is targeting the main query of the blog and archive page, and is limited to the front end. The query loop block should not be affected.
I tested the code on my site, doesn’t seem to be having the same issue.
If you remove the code from your site, does it fix the issue?
August 3, 2022 at 11:17 am #2302350Alessandro
I just tried removing the code from a dev copy of the site but there’s no change. I also disabled the caching plugin.
August 3, 2022 at 11:21 am #2302353Ying
StaffCustomer SupportI think the code is not the cause then.
Do you have any other functions? If so, can you try disabling all of them to test?
August 3, 2022 at 11:24 am #2302357Alessandro
There are 2 other functions, I removed them but the problem persists.
The strange thing is that this was all working normally when first building the site only a few weeks ago. The query loop block worked perfectly. Now it doesn’t anymore, but nothing changed. Maybe a GP/GB plugin update?
August 3, 2022 at 11:48 am #2302379Ying
StaffCustomer SupportIt should not be caused by GP/GB update, otherwise, we’ll receive a lot of reports, but we didn’t.
It’s likely something happened on your end.
Can you disable all plugins except GPP and GB to eliminate plugin conflicts?
If disabling plugins doesn’t work, can you provide the admin login to your site so we can take a look at the backend?
August 3, 2022 at 11:52 am #2302387Alessandro
I’m sending credentials in the private info field!
August 3, 2022 at 12:27 pm #2302423Ying
StaffCustomer SupportIt’s fixed by converting the block to HTML, then converting it back to block.
It seems a glitch.
August 3, 2022 at 12:28 pm #2302424Alessandro
OK, that is odd… I wouldn’t have thought to do that on my own. However the block seems to no longer be a Query Loop? It seems to just be a regular grid now.
August 3, 2022 at 1:53 pm #2302485Ying
StaffCustomer SupportOops…that’s true…I’ve reverted it back.
So it seems the no result issue is caused by the
order by menu order
parameter added to the query loop block, if we remove this parameter, the backend will work normally.As menu order is for pages usually, even we use the code to add it to the posts editor in admin, but the function that generates the preview of GB query loop doesn’t know.
I would suggest:
1. Remove the order by parameter from the query loop block.
2. Modify your PHP code to this:
add_action( 'admin_init', function() { add_post_type_support( 'post', 'page-attributes' ); } ); add_action( 'pre_get_posts', function( $query ) { if ( ! is_admin() ) { $query->set( 'orderby', 'menu_order' ); $query->set( 'order', 'ASC' ); } } );
August 3, 2022 at 4:22 pm #2302553Alessandro
Still the same problem… It’s not the end of the world if this can’t be resolved, at worst I can just change the sorting order in the editor while I make styling changes and then switch it back.
August 3, 2022 at 5:45 pm #2302606Fernando Customer Support
Hi Alco,
Instead of the code you have, can you try this?
add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) { if ( ! is_admin() && ! empty( $attributes['className'] ) && strpos( $attributes['className'], 'my-query' ) !== false ) { $query_args['orderby'] = 'menu_order'; } return $query_args; }, 10, 2 );
Just add
my-query
to the class list of the Grid Block within the Query loop block.August 3, 2022 at 5:50 pm #2302609Alessandro
I tried it on both the grid container and the post template, and it didn’t work.
-
AuthorPosts
- You must be logged in to reply to this topic.