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.
37 comments
jean-paul
Hi Can I define my own ID. Like mycourses, so that i can copy the section without the #brxe-xsdrea changing? Thank you
Sridhar Katakam
Not as far as I know. The Bricks ID of an element won't change.
Oliver Cole
Thank you for the tutorial! How would I make it so that it hides the section fully if no query element is retrieved. I have Service areas section within a single post template for my service areas post types and I want it so that the last child in my service area doesn't get displayed as a section. So here is my structure: England (show section)
-- West London (I want to hide this section)
Sridhar Katakam
Your site's structure/question is not clear. Can you share a screencast explaining your setup and what you are trying to do?
Mounika
It works only in Single post template, but not in archive page. Like Countries and Cities both are CPTs Now country has multiple cities. In archive page of countries (it shows all the countries) how to get how many cities that particular country has?
Sridhar Katakam
How are both CPTs related?
Mounika
ACF Bi-Directional relationship fields
Sridhar Katakam
Do you have a nested query loop for showing cities for each country?
mounika
In single country page I have a loop to display all the related cities, here it works and showing the count.
But in archive page I have a loop that displays all the countries, no need to show the countries only want to show count. So no nested query loop. and thanks for your reply.
Sridhar Katakam
in this case, it is not possible without custom code.
You need to write a custom function that returns the count of related cities for the current country loop iteration and output it using Bricks' echo dynamic data tag.
Sridhar Katakam
I wrote a new tutorial for you. https://brickslabs.com/acf-relationship-count-of-related-posts/
Cheok Way Leong
Hi Sridhar,
I think the default 0 does not work with Metabox Repeater field group as well, could you make an article for this if possible?
Cheok Way Leong
An update on trying on Metabox AIO's Relationship field & Repeater Group field query loops. This image is the element structure (https://prnt.sc/ipXS0-In3CKZ), 0 is set.
If i set 0 using the Relationship field query loop bricks-id, it does work, but it did not hide empty data, I assume it would be targeting the function whether this single post is linked to another post with relationship.
If i set 0 using the Metabox Repeater Field Group bricks-id, it does not work at all.
Cheok Way Leong
Hi Sridhar,
Wonder currently is it possible to 0 on the query loop which is a Metabox Group Field that is linked from another Relationship CPT? Meaning it's a double query loop set up, where parent loop is Relationship Query Loop and the child loop is a Group Query Loop from another CPT, but 0 is targeting the child loop.
Sridhar Katakam
Follow https://brickslabs.com/getting-field-values-from-a-connected-cpt-when-using-meta-box-relationship-in-bricks/.
eins walter
Thanks for updating. May I ask you found the way if the 0 can be used with out :hmvylb part for the self element with loop?
eins walter
0 meant "0" And "0" works that way. Just checked
Aljaz Bizjak
Doesn't seem to work if I use the Bricks Extras FAQ element via ACF query loop.
Sridhar Katakam
By BricksExtras FAQ element, you mean Pro Accordion?
Wernich Kay
Not sure if it's the same. But for others that stumble on this. I used this on Brix Extras Pro Tabs and it worked brilliantly. I put the condition on the "Tab Item" and targeted the empty element ID.
Question for Sridhar. In my case it was just to check if there is any content (not a specific count). I also tried {query_results_content:id} which worked. Would you say if you need a yes/no result {query_results_content:id} is fine to use?
Sridhar Katakam
Yes.
Matt
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.
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.
Sridhar Katakam
Are you using a query loop to output the featured products or a Products element?
Barnabas Chris
Fantastic solution!
Philipp
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?
Philipp
It works now, think it was a server problem on my side
Cheok Way Leong
Hi Philipp, I also stumbled upon this problem, at first the conditions does work properly. But after a few hours, it does not work anymore. How do you resolve this?
Philipp
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
Patrick
Thank you for the explanation. Has helped me a lot! Very powerful.
Benny
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
Sridhar Katakam
Hi Ben. WP comes with 2 post types (for managing content) -
pageandpost.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
pagepost 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.
joe
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?
Sridhar Katakam
The screenshot is when there are 0 published posts.
That hmvylb ID would be for the li elements (if present) inside the ul.