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

20 comments

Leave your comment

 

Related Tutorials..

How to conditionally load your scripts based on an ACF field

How to conditionally load your scripts based on an ACF field

This tutorial provides the PHP codes that can be pasted in order to enqueue your scripts conditionally based on the value of an ACF field…
Categories:
Pro
Check If The Latest Post Was Published in The Last X Minutes

Check If The Latest Post Was Published in The Last X Minutes

In this Pro tutorial we shall define a custom function that takes in the number of minutes as an argument and returns true if there…
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
Conditional Related Posts in Bricks

Conditional Related Posts in Bricks

How to output a section only if there is at least 1 related post for the current post being viewed.
Categories:
JetEngine Checkbox Condition in Bricks

JetEngine Checkbox Condition in Bricks

How elements in Bricks can be conditionally output depending on the selected option from a JetEngine checkbox field.
Categories:
Tags:
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:
Checking if the current post has been published within the last x days

Checking if the current post has been published within the last x days

Conditionally output an element if the current post has been published in the last x number of days.
Categories:
Pro
Conditional CSS in Bricks based on Post Meta

Conditional CSS in Bricks based on Post Meta

In the Bricks Facebook group a user asked: Hi, how do you normally control margin, padding and font-size using dynamic data? I'm looking for the…
Pro
User Role Conditional Output in Bricks

User Role Conditional Output in Bricks

This Pro tutorial provides the steps to output elements based on their CSS class to users of the specified role in WordPress when using Bricks…
Categories: