Is there a way to use an Array for a Query Loop

Hey guys, looking at the Custom Queries for the Loop Builder, it appears that you have to return a WP_Query for the Loop to work.

Is that correct, or is there a way to return a simple Array to use with the loop?

E.g. this could be data from a custom table or API call.

Creating a custom query you can actually fetch and loop through whatever you like:

  1. Add a custom query type
  2. Fetch the results however you like
  3. Loop through the results and prepare them for use
1 Like

Ah OK,

Do you have an example of how to access the values for dynamic data?

Say if I have an array like
[
[“name”=>“Bob”, “phone”=>“1234567”],
[“name”=>“Peter”, “phone”=>“1234567”],
]

How would I set up and access the values for “name” and “phone” ?

I’m thinking if the dataset is not Posts.

  • Perhaps I need to define a global
  • Set the global value on the ‘bricks/query/loop_object’
  • use {echo:get_my_global_val(key)}

Something to try a bit later.

You could set up a custom query like this:

add_filter( 'bricks/setup/control_options', function( $control_options ) {
    $control_options['queryTypes']['my_query_type'] = esc_html__( 'My Query Type', 'my-plugin' );
    return $control_options;
} );

add_filter( 'bricks/query/run', function( $results, $query_obj ) {
    if ( $query_obj->object_type !== 'my_query_type' ) {
        return $results;
    }
    
    return [
        [
            'name' => 'Bob',
            'phone' => '1234567'
        ],
        [
            'name' => 'Peter',
            'phone' => '7654321'
        ],
    ];
}, 10, 2 );

add_filter( 'bricks/query/loop_object', function( $loop_object, $loop_key, $query_obj ) {
    if ( $query_obj->object_type !== 'my_query_type' ) {
        return $loop_object;
    }

    /* Usually you'd prepare a WP_Post object or something
     * in here (see official documentation).
     * In this specific case there is nothing to do here.
     */
    
    return $loop_object;
}, 10, 3 );

To access the values in Bricks you can use the echo tag with a small helper function like this:

function get_custom_loop_object_property( $name ) {
    $loop_object = \Bricks\Query::get_loop_object();
    if ( ! $loop_object ) return false;
    if ( ! is_array( $loop_object ) ) return false;
    if ( ! array_key_exists( $name, $loop_object ) ) return false;
    return $loop_object[$name];
}

Then you can call it within your query loop using {echo:get_custom_loop_object_property(name)} or {echo:get_custom_loop_object_property(phone)}.

I hope there will be an easy (and documented) way to create custom dynamic tags soon. I’d prefer those over using {echo:xyz} all the time. So @timmse, if you find the time… :sweat_smile:

13 Likes

Ah brilliant. I was not aware of
\Bricks\Query::get_loop_object();

That makes everything make sense now. Thank you for your efforts.

3 Likes

Hi aslotta
Thank you for this code!

I’m trying to return results from a json file. It works if I use return json_decode($file, true).
But I can’t figure out how I could define the maximum number of items, similar to posts_per_page. I’ve tried array_slice, but then I can’t use a load more button.

Do you have any advice?
Thank you very much!

I’ve been looking for this instruction for a long time. Why isn’t this in academy :frowning:

4 Likes