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 – sorting by ACF field

  • In Generateblocks v1, I have a query loop. On the grid located below the query, I have set the CSS class My-order, which sorts the posts in the list according to the ACF field Datum. The code is as follows:

    add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) {
        // apply filter if loop has class: my-order
        if (
            ! empty( $attributes['className'] ) &&
            strpos( $attributes['className'], 'my-order' ) !== false
        ) {
    $query_args['orderby'] = 'meta_value';
    $query_args['meta_key'] = 'datum';
    $query_args['order'] = 'ASC';
        }
    
        return $query_args;
    
    }, 10, 2 );

    In GB v2, when I put the My-order class on Looper, this code does not work and I cannot target it correctly to make it work. Can you help, please?

  • Hi there,

    1. Add the my-order class to the query block in v2.

    2. and change generateblocks_query_loop_args to generateblocks_query_wp_query_args.

    Let me know if this helps!

  • You are using the Inherit query from template option, so the order will be inherited from the default template. This code cannot change its order.

  • In GB v1, it worked as inherited from the template. When I turn it off, it works.

  • I tried this code and it works with the settings inherited from the template.

    add_action( 'pre_get_posts', function( $query ) {
    
        if ( is_admin() || ! $query->is_main_query() ) {
            return;
        }
    
        // jen archiv a pozvánky
        if ( ! is_category( [ ID, ID2 ] ) ) {
            return;
        }
    
        $query->set( 'orderby', 'meta_value' );
        $query->set( 'meta_key', 'datum' );
        $query->set( 'order', 'ASC' );
    
    });
  • Yes, that’s a better approach to adjust the order for the posts themselves, instead of adjusting the order at the query block level.

  • Thank you for help 🙂

  • You are welcome   🙂

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