Site logo

Archived topic

Custom "order by" prevents Query Loop form loading in backend

28 replies · Started by Alessandro on August 3, 2022

Viewing posts 1–15 of 29

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}}}}

Hi 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?

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."

That'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?

I just tried removing the code from a dev copy of the site but there's no change. I also disabled the caching plugin.

I 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?

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?

It 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?

I'm sending credentials in the private info field!

It's fixed by converting the block to HTML, then converting it back to block.

It seems a glitch.

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.

Oops...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' );
}
} );

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.

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.

I tried it on both the grid container and the post template, and it didn't work.

This archived topic is closed to new replies.