8th Nov '22
/
20 comments

Conditionally Outputting Based on Query Count in Bricks

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

Get access to all 626 Bricks code tutorials with BricksLabs Pro

20 comments

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

    Thanks, David

  • 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: https://pastebin.com/1Uhze2gN 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 ?

    Thanks

    I

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

    Thanks!

  • 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? Thanks

Leave a Reply to Darion (Cancel Reply)

 

Related Tutorials..

Conditionally Outputting Elements only for Posts that have a Specific Taxonomy Term

Conditionally Outputting Elements only for Posts that have a Specific Taxonomy Term

Using the core WordPress has_term() function for checking if the current post has a specific term of a given taxonomy.
Categories:
Conditional Rendering for Logged in/out Users by CSS Classes in Bricks

Conditional Rendering for Logged in/out Users by CSS Classes in Bricks

This tutorial shows how Bricks builder's bricks/element/render filter can be used to conditionally output elements having a class of logged-in to logged-in users only and…
Categories:
Conditional Output based on Manual Excerpt in Bricks

Conditional Output based on Manual Excerpt in Bricks

How to display a post’s excerpt only if it has been manually set.
Categories:
Pro
Checking if the current Page/Post has Children i.e. is a Parent in Bricks

Checking if the current Page/Post has Children i.e. is a Parent in Bricks

Shows how we can check whether the current Page or Post (of any hierarchical post type) is a parent
Categories:
Is WooCommerce Cart Empty Condition in Bricks

Is WooCommerce Cart Empty Condition in Bricks

Updated on 5 Nov 2024 Looking to conditionally render an element depending on whether the user's cart is empty or not when using WooCommerce? Add…
Conditional Output on the First Page of Archives in Bricks

Conditional Output on the First Page of Archives in Bricks

How to output an element on an archive page only if we're on the first page of the archive.
Categories:
[Function] Current Term Has Children

[Function] Current Term Has Children

Updated on 20 Jul 2024 A user asks: I'm working on an archive template for WooCommerce products. I'm trying to hide a section if there…
Pro
Conditional single post Bricks template based on internal referrer URL

Conditional single post Bricks template based on internal referrer URL

How to automatically select a single post template depending on the page from which the post is accessed.
Pro
Month and Day Condition in Bricks

Month and Day Condition in Bricks

This Pro tutorial shows how we can define a custom function that takes in a month in the three-letter format (like Dec) and a day…
Categories: