22nd Sep '23
/
37 comments

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 610 Bricks code tutorials with BricksLabs Pro

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

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

    • London (show section)

    -- West London (I want to hide this section)

    • A

      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?

    • A

      How are both CPTs related?

      • Mounika

        ACF Bi-Directional relationship fields

        • A

          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.

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

    • A

      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?

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

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

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

Pro
Post Titles and Post Content Tabs in Bricks

Post Titles and Post Content Tabs in Bricks

How to set up Nestable tabs with post titles as the tab menu, and post content and/or any other post-specific data as the tab content.
Categories:
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…
Pro
Displaying Gallery of SureCart Product Images with Bricks’ Query Loop

Displaying Gallery of SureCart Product Images with Bricks’ Query Loop

Custom query loop for looping through SureCart product images.
Categories:
Tags:
Pro
Filtering posts by a group field’s subfield value with Meta Box when using a custom table in Bricks

Filtering posts by a group field’s subfield value with Meta Box when using a custom table in Bricks

Filtering a query loop to output only those posts with the value of a Select subfield of a Group field set to Yes when using…
Categories:
Tags:
Pro
Posts Grouped by Taxonomy Terms in Bricks using BricksExtras’ Dynamic Table and Pro Tabs

Posts Grouped by Taxonomy Terms in Bricks using BricksExtras’ Dynamic Table and Pro Tabs

How to output any post type, with the taxonomy terms as Tab titles and posts inside tables with sortable columns.
Pro
Post-specific ACF Repeater Galleries as Sliders in Bricks

Post-specific ACF Repeater Galleries as Sliders in Bricks

How to output ACF Repeater field rows with the images of the Gallery-type sub field as a slider.
Pro
CMB2 Repeatable Group Bricks Query Loop

CMB2 Repeatable Group Bricks Query Loop

This Pro tutorial provides the steps to create a custom query loop type for outputting the subfield values of a Group field (repeatable) when using…