Conditionally Outputting Based on Query Count in Bricks

Update on 22 Sep 2023: There’s a much simpler built-in method now. See this post.

In the Bricks Facebook group a user asks:

Is there a way, either using dynamic data or a custom PHP solution, to render an element based on if another element has content?

I’m sure there is, I just can’t get my head around it.

Use case:

I’m trying to use the Nestable Tabs element to display dynamic data. One of the tabs is using a query loop. If the query has no content/data, I want to hide the associated tab completely. I don’t want to use CSS to display: none – I want to complete stop the render of the tab.

I don’t mind using a custom class on the query loop or some a data attribute or something – but I can’t get it to work. Any pointers?

This can be done by creating a manual query with the same query parameters as what’s set in the builder, getting the count and checking against that using the Dynamic data condition.

The query in this example:

Step 1

Add the following in your child theme’s functions.php or a code snippets plugin:

function bl_get_posts_count() {
	// custom wp_query
	$args = array(
		'post_type' => 'post',
		'posts_per_page' => -1, // all matching posts
		'category__in' => array( 2, 6 ), // posts that have these categories (not children)
		'meta_query' => array(
				'key' => 'related_bios',
				'value' => get_the_ID(),
				// 'compare' => 'LIKE',

	$posts = new WP_Query( $args );

	// return the count of posts
	return $posts->found_posts;

Step 2

Apply this condition to the element(s) that you wish to be output only if there is at least 1 post that matches our query.

Instant access to 390+ Bricks code tutorials with BricksLabs Pro


  • Hi Sridhar,
    I am trying to do this where i have a custom post trype called "events" i want to hide a section if there are no event posts.
    Is this the same PHP that you have provided above? Do i need to alter it?


  • Eric Embacher

    Can you suggest how to do this when the query loop is querying an ACF repeater rather than posts? So if a post has an ACF repeater field with multiple elements in the repeater, how can I count the number of elements in the reparter field for that post?

  • David Demastus

    So I'm querying some events in their own section inside a Taxonomy archive template. This taxonomy is essentially a global list of terms that applies to all my post types, so if someone accesses a specific terms archive page, 4-5 sections will populate content from various custom post types that share the same term. Since I have the query on an archive template, it automatically selects the term for me. So I simply chose Events as my post type for the query, and the template does the rest of the work pulling in the events related to the term archive.

    How would I target this event Section to be hidden if there are no events with that term?

  • Eric Embacher

    I'm trying to do the same thing but show or hide a block based on the count of posts related to the current post via an ACF relationship field (show show block A if there is a single related post, show block B if there are multiple related posts).
    The field name of relationship field is "associated winery", so my code is as follows:
    It doesn't work, and on the front end neither block gets output. Do I need to change something else to make this work with relationship fields?

  • Hello

    I am trying to achieve the same thing but with the "related product" element. The “related posts” element is a query loop that shows the feature image of Woocommerce products in a similar category or tag. How can I hide the Div completely if the query returns no products?

    I am really stuck here, I have tried your solution but I cannot adapt it to the related posts element.

    Could you help me out please ?



  • Tiago Soarez Loureiro

    Hey, awesome work,

    what about if I need to set a condition for an author's page archive, where I want to show a section with inside a query loop of the author's latest posts, but don't want it to render if the author has no posts? I am trying to use your code, but I suck at php.


  • Hi! How can I alter this function to exclude meta query? I have a custom post type that has two terms, "open status" and "closed status." I am trying to use the echo dynamic tag to display total post count, post count for open status, and post count for closed status.

    I'm assuming I will need to add 3 different functions to code snippets, but do not know how to write these. Your help would be beyond appreciated.

  • Great tut,
    If I’m using custom post type(cpt plugin) it will work?

Leave your comment