NO BUG: "bricks/query/run" filter doesn't work for Carousel

I can modify the query of a DIV element with the “bricks/query/run” filter, but I can’t modify it for the CAROUSEL element.
Is this on purpose? Any chance to make this happen?

Hey @jstneti,

you mean you want to dynamically output images in the carousel element? As you are not using a query to populate the carousel element you do not use a hook here. But you can write a custom function to populate it instead:

function my_carousel_images() {
    return [ 93, 91 ]; // Return an array with the image IDs here
}

add_filter( 'bricks/code/echo_function_names', function() {
    return [
        'my_carousel_images',
    ];
} );

You can then use this function in the element:

CleanShot 2024-09-04 at 09.31.47

Best,

André

No. I use it to display blog posts.

The issue I have is that I need to show max 4 latest blog posts including sticky posts. But the default WP_query doesn’t work like that.
So, I used two queries so that it works in all those cases:

  • 4 sticky posts → display only 4 sticky posts
  • 2 sticky posts → display 2 sticky posts + 2 normal posts
  • 0 sticky posts → display 4 normal posts

Got it. In this case you can use the bricks/posts/query_vars hook I guess:

add_filter( 'bricks/posts/query_vars', function( $query_vars, $settings, $element_id ) {
    if ( $element_id !== 'YOUR_CAROUSEL_ID_HERE' ) {
        return $query_vars;
    }

    // Adjust the query to your needs here

    return $query_vars;
}, 10, 3 );

Depending on how exactly your logic looks like you can also just use the custom function approach from my first comment and perform the query in there.

Best,

André

I managed to get it working by using a Nested Slider instead of the Carousel.

FYI, here is the query I use, in case anyone finds it useful:

$posts_per_page = 4;

    // get sticky posts first:
    $sticky_posts = get_option('sticky_posts');
    $sticky_posts_query = new WP_Query([
        'post__in' => $sticky_posts,
        'posts_per_page' => $posts_per_page,
        'ignore_sticky_posts' => 1,
        'orderby' => 'date',
        'order' => 'DESC',
    ]);
    $sticky_posts_count = $sticky_posts_query->post_count;
    $additional_posts_count = $posts_per_page - $sticky_posts_count;
	
	// If less than 4 sticky posts, get more non-sticky posts
	if ( $additional_posts_count > 0 ) {
		$additional_posts_query = new WP_Query([
			'post__not_in' => $sticky_posts, // Exclude sticky posts
			'posts_per_page' => $additional_posts_count,
			'ignore_sticky_posts' => 1,
			'orderby' => 'date',
			'order' => 'DESC',
    	]);

		// merge
		$latest_posts = array_merge($sticky_posts_query->posts, $additional_posts_query->posts);
		return $latest_posts;
	}
	
	// If 4 or more sticky, show only sticky
	return $sticky_posts_query->posts;

Hey @jstneti,

I’ll mark this as a no-bug, since it’s not really a bug.

I see that you got it working, so nice to hear that! :partying_face: Your solution is totally fine, just like it would be fine the one that @aslotta posted above with custom function.

But, if you want to modify query result, then you can also use bricks/query/result filter.

Anyway, I’m happy that it works now :wink:

Best regards,
M