Query Results Count in Bricks

Bricks 1.9.1 added a {query_results_count:bricksid} dynamic data tag that can be used to conditionally render Sections (or any element) depending on whether the contained (specified) query has at least one result.

We can now get the query result count of any query based on the Bricks ID.

Previously, the only way to do this was to create another query with the same parameters manually in a custom function and use it with the Dynamic data condition as demonstrated here.

query_results_count by itself is alright for showing the query results count inside the loop, but {query_results_count:bricksid} is the gold here especially because it works even outside the loop. This is truly a game-changer and something advanced Bricks users highly appreciate.

Let’s say you have 5 latest posts of a CPT showing up like this:

via this structure:

with the query set to:

When the number of published posts is 0, the entire Section will still be output incl. the heading:

To ensure that the Section gets output only when there is at least 1 published post (of the project CPT, in this example) we just need to apply a Dynamic data condition like this:

{query_results_count:hmvylb}

where hmvylb is the Bricks ID of the element on which query loop is enabled.

This works with all query types incl. ACF Repeater and not just the default, Posts.

Combined with the PHP query editor with support for dynamic data, Bricks v1.9.1 is a significant update for developers.