Generateblocks_query_loop_args

Hi - hope someone (Tom?) can answer my question. I’ve been playing around with the generateblocks_query_loop_args filter, and I’ve been writing it like this:

add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) {
    if ( ! empty( $attributes['className'] ) && strpos( $attributes['className'], 'custom-grid-class' ) !== false ) {
        $query_args['meta_key'] = 'field_name';
        $query_args['orderby'] = 'meta_value';
        $query_args['order'] = 'ASC';
    }

    return $query_args;
}, 10, 2 );

What I’d like to know - is there a way I can write my query arguments like this (for example):

$query_args = array(
      'meta_key'  => 'field_name',
      'order_by' => 'meta_value',
       'order' => 'ASC'
    );

   ...

I’m asking if I can somehow write the query arguments in a more efficient manner - I’m used to not having to write “$query_args[XXXX]” over and over again - basically can I somehow drop the first part of that (bolded). I’m not incredibly familiar with PHP so be gentle. :slight_smile:

Thanks!

For anyone coming back to this thread, Tom responded to me in a support ticket. Adding his response here (hope that’s okay!):

Hi Cody,

You can do that, but you need to:

  1. Name the array something else: $custom_args = array();
  2. Merge the two at the end: return array_merge( $query_args, $custom_args );

Hope this helps!

So the code above now looks like this (and works perfectly):

add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) {
    if ( ! empty( $attributes['className'] ) && strpos( $attributes['className'], 'custom-grid-class' ) !== false ) {
        $custom_args = array(
          'meta_key' => 'last_name',
          'orderby' => 'meta_value',
          'order' => 'ASC',
        );
    }
    return array_merge( $query_args, $custom_args );
}, 10, 2 );
1 Like

@Tom

is there a plan to build something like Developers - Documentation for the GenerateBlocks, so there is an official resource where one can find available hooks, filters and methods like the one above or block defaults, styles etc that can be controlled via code?

On the GeneratePress side of things having this section in the docs is extremely helpful.
Is it only the matter or not having enough time to do it, or you do not plan to?

Thanks

At this point it’s just a time thing - I’d absolutely like to.

I haven’t done so because I don’t love the idea of filtering defaults - it tends to cause more headaches than it solves.

Now that we’re getting more PHP filters in the plugin (other than defaults), a section like that is becoming more of a necessity.

1 Like