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.

Get access to all 524 Bricks code tutorials with BricksLabs Pro

17 comments

  • Aljaz Bizjak

    Doesn't seem to work if I use the Bricks Extras FAQ element via ACF query loop.

  • Thank you very much for this post!

    I was trying to use this dynamic data tag myself and it wasn't working when I was putting the whole section id, instead only what's after the "-".
    Thanks to the last image in your post I got it 🙂

    Thank you for your post and detailed guidance. 🙏

  • Michael van Dinther

    Hi Sridhar,

    Is it possible to use query_results_count to determine if the result is an odd number? (i.e 3, 5 or 7).

    Specific scenario : show a placeholder in a grid layout if the query only has odd numbers. The placeholder fills the remaining space.

  • 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:
    https://academy.bricksbuilder.io/article/query-loop/#query-editor

    https://academy.bricksbuilder.io/wp-content/uploads/2023/09/bricks-1.9.1-query-editor-php.png

  • 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.

    Sincerely
    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

 

Related Tutorials..

Adding any Custom WP_Query loop to Bricks’ Query Loop

Adding any Custom WP_Query loop to Bricks’ Query Loop

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…
Using WP Grid Builder Facets with Bricks’ Query Loop

Using WP Grid Builder Facets with Bricks’ Query Loop

Update: This tutorial was written before WPGB had a Bricks add-on. Now, it is advisable to use that add-on instead. Get it from here. Updated…
Categories:
Pro
Current Single Post Reference From Inside a Bricks Query Loop

Current Single Post Reference From Inside a Bricks Query Loop

Use case: Get the single post’s custom field (like ACF Repeater or Meta Box Group) array row corresponding to the loop’s counter/index
Categories:
Pro
Querying Posts by ACF Date Sub Field in Bricks

Querying Posts by ACF Date Sub Field in Bricks

Updated on 17 Nov 2023. This Pro tutorial shows how we can set post__in query parameter of a query loop in Bricks to an array…
Categories:
Tags:
Bricks Templates Query Loop

Bricks Templates Query Loop

How display Bricks Templates with saved screenshots in a query loop.
Categories:
Pro
Conditionally Outputting Query Loop Item Based on Post Meta in Bricks

Conditionally Outputting Query Loop Item Based on Post Meta in Bricks

Rendering query-loop enabled posts depending on the value of each post's custom field value is tricky because by default, the custom field plugins' functions or…
Categories:
How to Show Future Events in Bricks

How to Show Future Events in Bricks

One of the common requirements in WordPress is being able to filter the events (or could be posts of any post type) to only those…
Categories: