Are you a GenerateCustomer?

Do you have an active GP Premium or GenerateBlocks Pro license key?

Create a GenerateSupport account for GeneratePress and GenerateBlocks support in one dedicated place.

Create an account
Already have a GenerateSupport account? Login

Just browsing?

Feel free to browse the forums. Support for our free versions is provided on WordPress.org (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

Query loop order by id

  • Hello,
    I want to order a query loop by ascending id, so I inserted the taxonomy and selected
    order by id and order ASC
    In the editor everything looks fine, but in the frontend I see it ordered by published date ascending. So I’m wondering what is wrong. Can the parameters order by and order be used together?

    I cleared the complete site cache, browser cache, but no success.

    The query is below the title
    https://www.tourist-in-rom.com/en/rome-autumn/#whats-going-on-in-autumn-in-rome

    Thank you

  • Hi there,

    it “should” work, but it may be a bug. Ill do some tests on a local dev site to see if I can replicate it.

  • Hi David,
    I prepared access and excluded the url from cache.
    I hope this is useful.

  • Hi there,

    I was able to replicate the issue on my testing site, which seems to be a bug. However, there’s a workaround you can use if you’d like.

    Edit the query loop and add the class order-by-id to the Grid block under Advanced > Additional CSS Classes.

    Then, add this snippet:

    add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) {
        if ( ! is_admin() && ! empty( $attributes['className'] ) && strpos( $attributes['className'], 'order-by-id' ) !== false ) {
    		
           $query_args['orderby'] = 'ID';
           $query_args['order'] = 'ASC';
        }
        return $query_args;
    }, 10, 2 );

    Adding PHP: https://docs.generatepress.com/article/adding-php/

  • Hi Alvind,
    it works, thank you 🙂
    The strange thing is that it does not work inserting the snippet as a hook in wp_footer, but it works inserting it in the child theme.
    What am I doing wrong? The link to the hook is in the private information.

  • The snippet is in PHP, so it won’t work if added via hooks like that.

    Adding it to the functions.php file of your child theme is the correct method.

  • OK, thank you 🙂

  • You’re welcome 🙂

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