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 610 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 your comment

 

Related Tutorials..

Pro
How to Insert Element(s) Between Query Loop Posts in Bricks

How to Insert Element(s) Between Query Loop Posts in Bricks

Update on 16 Aug 2023: Follow this tutorial instead. This Pro tutorial shows how we can insert a Div (or any custom HTML) after nth…
Pro
Custom Conditions in Bricks Builder

Custom Conditions in Bricks Builder

Updated on 22 Jun 2022 This Pro tutorial is a continuation of the previous Conditions in Bricks Builder tutorial. We are going to keep updating…
Categories:
Pro
Day of Week Condition in Bricks

Day of Week Condition in Bricks

Displaying elements conditionally based on the current day of the week.
Categories:
Conditionally Hide a Section on a Specific Taxonomy’s Term Archives in Bricks

Conditionally Hide a Section on a Specific Taxonomy’s Term Archives in Bricks

A user asked: I have a CTA in the footer of my site, which I have custom fields on various templates, etc to populate. I…
Categories:
Pro
Conditional Rendering Based On Meta Box Group’s Sub Field

Conditional Rendering Based On Meta Box Group’s Sub Field

This Pro tutorial shows how we can output an element on a single post pages only if a specified sub-field (custom field created using Meta…
Pro
Conditional Rendering of ACF Repeater Rows Based on True / False Sub Field in Bricks

Conditional Rendering of ACF Repeater Rows Based on True / False Sub Field in Bricks

This Pro tutorial provides the steps to modify a ACF Repeater query loop to only output the rows for which a True / False type…
Categories:
Pro
Displaying Related Post Content on a Category Archive Page in Bricks

Displaying Related Post Content on a Category Archive Page in Bricks

In this Pro tutorial for Bricks users, we shall Use case: This method is useful when you want to associate or link a particular post…
Categories:
Pro
Showing a Popup conditionally in Bricks based on Query Parameter

Showing a Popup conditionally in Bricks based on Query Parameter

Here's two ways a Bricks Popup can be shown on page load depending on URL parameter value.
Categories:
Tags:
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: