50% discount on BricksLabs Pro Membership Lifetime Access. Use coupon: GOBRICKS

Adding any Custom WP_Query loop to Bricks’ Query Loop

wp_query bricks

Sometimes we may need to create a custom post loop using Bricks’ query loop feature, but wish to use our own WP_Query code for more flexibility.

This could either be because we need to do something that the built-in settings don’t quite allow for (needing something more dynamic for some of the WP_Query arguments), or maybe we just already have the code on hand for a fairly complicated post query and it’s just quicker pasting in the code than manually having to rebuilding it all through the UI.

Using Bricks’ Query Filters

Bricks comes with a few filters that allow us to change how different features behave and these make this possible.

The developer documentation is a great resource for finding out about all these filters individually. Here we’ll be using some of them together to add our new query type and inserting some custom WP_Query arguments for the Query loop feature to use. (see the links at the end of this post for information on the specific filters being used)

Adding the code snippet

This code would need to be added outside of the builder, either inside your child theme’s functions.php file or a code snippet manager such as WPCodeBox.

The code below takes care of..

  • Adding a new setting in the ‘type’ dropdown of the Query loop control
  • Running a new query if that setting is chosen (our WP_Query code)
  • Set up global post data
/* Add new query type control to query options */
add_filter( 'bricks/setup/control_options', 'bl_setup_query_controls');
function bl_setup_query_controls( $control_options ) {

    /* Adding a new option in the dropdown */
    $control_options['queryTypes']['my_new_query_type'] = esc_html__( 'My new WP Query' );

    return $control_options;

};

/* Run new query if option selected */
add_filter( 'bricks/query/run', 'bl_maybe_run_new_query', 10, 2);
function bl_maybe_run_new_query( $results, $query_obj ) {
	
    if ( $query_obj->object_type !== 'my_new_query_type' ) {
	    return $results;
    }
    
    /* If option is selected, run our new query */ 
    if ( $query_obj->object_type === 'my_new_query_type' ) {
        $results = run_new_query();
    }
    
    return $results;

};


/* Setup post data for posts */
add_filter( 'bricks/query/loop_object', 'bl_setup_post_data', 10, 3);
function bl_setup_post_data( $loop_object, $loop_key, $query_obj ) {
    
    if ( $query_obj->object_type !== 'my_new_query_type' ) {
	    return $loop_object;
    }

     global $post;
     $post = get_post( $loop_object );
     setup_postdata( $post );
    
     return $loop_object;

};


/* Return results from our custom WP Query arguments */
function run_new_query() {

    /* Add all of your WP_Query arguments here */

    $args = [ 
        'post_type' => 'post',
        'orderby'        => 'rand',
        'posts_per_page' => '1',
    ];
    
    $posts_query = new WP_Query( $args );

    return $posts_query->posts;

};

To customize it to make it your own query..

  • Change the query type my_new_query_type where it is mentioned in the first three functions to something more relavent.
  • Change the name from My new WP Query in the first function. (this will be how it appears inside of the settings)
  • Add your WP_Query arguments in the last function, inside the $args array.

That’s it

Now when you go into Bricks you’ll see the new option in the Query Loop type dropdown and can create your post loop.

Edit – See this Youtube video tutorial, by Ivan Nugraha, for an example of implementing this method to show posts based on ACF relationship fields – https://www.youtube.com/watch?v=JLCdFATDdvg

Update – Example code for adding multiple custom queries

Named ‘my_first_custom_query’ and ‘my_second_custom_query’ for easier reading..

/* Add new query type controls to query options */
add_filter( 'bricks/setup/control_options', 'bl_setup_query_controls');
function bl_setup_query_controls( $control_options ) {

    /* Adding new options in the dropdown */
    $control_options['queryTypes']['my_first_custom_query'] = esc_html__( 'My first WP Query' );
	$control_options['queryTypes']['my_second_custom_query'] = esc_html__( 'My second WP Query' );

    return $control_options;

};

/* Run new query if option selected */
add_filter( 'bricks/query/run', 'bl_maybe_run_new_queries', 10, 2);
function bl_maybe_run_new_queries( $results, $query_obj ) {
	
    if ( $query_obj->object_type === 'my_first_custom_query' ) {
        $results = run_first_query();
    }
	
	if ( $query_obj->object_type === 'my_second_custom_query' ) {
        $results = run_second_query();
    }
    
    return $results;

};


/* Setup post data for posts */
add_filter( 'bricks/query/loop_object', 'bl_setup_post_data', 10, 3);
function bl_setup_post_data( $loop_object, $loop_key, $query_obj ) {
    
    /* setup post data if using any of our custom queries */
    if ( $query_obj->object_type === 'my_first_custom_query' || $query_obj->object_type === 'my_second_custom_query' ) {
		
       global $post;
       $post = get_post( $loop_object );
       setup_postdata( $post );
		
    }
    
    return $loop_object;

};


/* first WP Query arguments */
function run_first_query() {

    /* Add all of your WP_Query arguments here */
    $args = [ 
        'post_type' => 'post',
        'orderby'        => 'rand',
        'posts_per_page' => '2',
    ];
    
    $posts_query = new WP_Query( $args );

    return $posts_query->posts;

};

/* second WP Query arguments */
function run_second_query() {

    /* Add all of your WP_Query arguments here */
    $args = [ 
        'post_type' => 'post',
        'orderby'        => 'rand',
        'posts_per_page' => '1',
    ];
    
    $posts_query = new WP_Query( $args );

    return $posts_query->posts;

};

Filter references:

https://academy.bricksbuilder.io/article/filter-bricks-query-loop_object/
  • Has this worked for folks? Any ideas as to why only my first function works, but not the second? The code is nearly identical!

    • A
      David Browne

      Hi Sarah,

      If you want to share a link to your version of the code I can take a look.

  • Hi. This works nicely, but what if I want to add more than one custom wp_query loop?

Leave your comment