Site logo

[Resolved] Custom “order by” prevents Query Loop form loading in backend

Home Forums Support [Resolved] Custom “order by” prevents Query Loop form loading in backend

Home Forums Support Custom “order by” prevents Query Loop form loading in backend

Viewing 15 posts - 1 through 15 (of 29 total)
  • Author
    Posts
  • #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}}}}

    #2302330
    Ying
    Staff
    Customer Support

    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?

    #2302334
    Alessandro

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

    #2302346
    Ying
    Staff
    Customer Support

    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?

    #2302350
    Alessandro

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

    #2302353
    Ying
    Staff
    Customer Support

    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?

    #2302357
    Alessandro

    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?

    #2302379
    Ying
    Staff
    Customer Support

    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?

    #2302387
    Alessandro

    I’m sending credentials in the private info field!

    #2302423
    Ying
    Staff
    Customer Support

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

    It seems a glitch.

    #2302424
    Alessandro

    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.

    #2302485
    Ying
    Staff
    Customer Support

    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' );
    }
    } );
    #2302553
    Alessandro

    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.

    #2302606
    Fernando
    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.

    #2302609
    Alessandro

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

Viewing 15 posts - 1 through 15 (of 29 total)
  • You must be logged in to reply to this topic.