Get Bricks

Filter: bricks/posts/query_vars

Since Bricks 1.3.2 you may manipulate the posts, products, or Query Loop elements query vars before the query is performed like so:

add_filter( 'bricks/posts/query_vars', function( $query_vars, $settings, $element_id ) {
    // Use an ACF custom field to restrict the query to a set of posts
    if ( $element_id == 'fhmnfx' && function_exists('get_field') ) {
        $query_vars['post__in'] = get_field('my_posts_acf_field');
    }

    return $query_vars;
}, 10, 3 );

The filter callback receives three arguments:

  • $query_vars an associative array used to feed the WP_Query class
  • $settings an associative array containing the element settings set in the builder
  • $element_id is a string containing the unique element ID (since Bricks 1.3.6)

Example: Loop images from Metabox.io Advanced Image Field of current post

add_filter( 'bricks/posts/query_vars', function( $query_vars, $settings, $element_id ) {

    // Only target yeamho element_id
    if ( $element_id !== 'yeamho') return $query_vars;
        
    // Get Metabox advanced images field values. 'mg_projet_galerie_images' is the field id
    $gallery_images = (array) rwmb_meta( 'mg_projet_galerie_images', ['size' => 'full'] );
	
    // Get the Images Ids only
    $gallery_images_ids = array_keys($gallery_images);
    
    // If no gallery images, set empty string array
    $gallery_images_ids = count( $gallery_images_ids ) > 0 ? $gallery_images_ids : [''];
    
    // Set the images ids as post__in parameters
    $query_vars['post__in'] = $gallery_images_ids;

    return $query_vars;
}, 10, 3 );

Example: Apply orderby argument with 2 different fields

Imagine you got a Performance post type with a start date field and a start time field. As you will not create the Performance post by actual sequence, you wish to order the Performance posts by start date (ascending) and start time (descending).

add_filter( 'bricks/posts/query_vars', function( $query_vars, $settings, $element_id ) {
    
    // Only target 3b03dd element_id
    if( $element_id !== '3b03dd') return $query_vars;

    // Set meta_query
    $query_vars['meta_query'] = [
        'relation' => 'AND',
        'performance_start_date' => array(
            'key' => 'performance_start_date',
            'compare' => 'EXISTS',
        ),
        'performance_start_time' => array(
            'key' => 'performance_start_time',
            'compare' => 'EXISTS',
        ), 
    ];

    // Set orderby
    $query_vars['orderby'] = [
        'performance_start_date' => 'ASC',
        'performance_start_time' => 'DESC'
    ];

    return $query_vars;
}, 10, 3 );