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:


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.

Instant access to 390+ Bricks code tutorials with BricksLabs Pro


  • Mr Tuan

    On my website, the products have no price.

    I only sell to dealers.

    There is one section that I put in Flash Sale. (This section only shows featured products).

    How to hide section when there are no featured products in Flash Sale.

    Thank you for your careful guidance.

    Thank you very much.

  • Barnabas Chris

    Fantastic solution!

  • I just used the dynamic tag to show a custom "No Results" Message with a sad face smiley. This works like a charm ๐Ÿ™‚ ๐Ÿ™‚

  • Philipp Stakenborg

    I have Problems making this work when working with a Metabox Relationship Querry. Did you stumble upon this problem?

    • It works now, think it was a server problem on my side

      • Also noticed: never rename the bricks id, otherwise the querry count does not work anymore.

  • Marcel Darvas

    I had trouble finding out how to use the "PHP query editor with support for dynamic data" with 0.

    Maybe linking or extending with an example could be beneficial:


  • Thank you for the explanation. Has helped me a lot! Very powerful.

  • Thank you, Shridhar

    I feel like an inescapable newbie. For some reason, the concept of CPTs and WP Posts is hard for me to wrap my head around.
    I am sure the information you generously provided here will eventually fill the gaps in my knowledge.

    Question - Is a post presented on a page or template and how is a template different from a page?
    And when would you use a regular WP Post that comes with WP over a CPT? (I'm learning Metabox)

    Any information or videos that would explain this for Brick's users would be appreciated.

    Ben M

    • A

      Hi Ben. WP comes with 2 post types (for managing content) - page and post.

      We can create our own post types (Custom Post Types) using code or plugins.

      Generally speaking, "page" refers to a webpage.

      When you are referring to an item of the page post type, you'd write it as "Page" or "a static Page".

      We can show a list of posts (regular posts or CPT's) on a static Page or in a template (typically, of the type Archive).

      CPTs are used to create specific types of posts. Ex.: Projects, Persons.

      Certain plugins like WooCommerce also create CPTs when they are activated. In the case of WC, it is product.

      A template is something that is typically used by more than 1 item of a given post type if the template type is single.

      If the template type is an archive, it can be applied to the category listing page or CPT archive page etc.

  • thanks for this article. In your screenshot where you are inspecting with dev tools, I dont see the ID hmvylb. Where is it in the DOM in your example? Also, if I use the ID of a Related Posts element rather than of a Block with a query enabled, shouldn't it work the same?

    • A

      The screenshot is when there are 0 published posts.

      That hmvylb ID would be for the li elements (if present) inside the ul.

Leave your comment