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 a Reply to Gul (Cancel Reply)

 

Related Tutorials..

Pro
Conditional CSS Classes based on Bricks Query Count

Conditional CSS Classes based on Bricks Query Count

In a Bricks project I am currently working on, there are nested query loops - team members inside departments on a Page. Each team member…
Checking if Repeater/Cloneable Meta Fields Are Empty in Bricks

Checking if Repeater/Cloneable Meta Fields Are Empty in Bricks

Creating a condition to ensure that a section only gets output if at least one row of data is filled in.
Categories:
Pro
Condition for checking Bricks content

Condition for checking Bricks content

Only outputting Bricks content when a Page is built with Bricks, otherwise display WP content.
Categories:
Pro
Bricks Dynamic Condition – Check if today falls within 5 days before any holiday date

Bricks Dynamic Condition – Check if today falls within 5 days before any holiday date

Creating a custom condition that returns true or false depending on whether today falls within 5 days before any holiday dates selected on a Meta…
Categories:
Tags:
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:
Pro
Conditionally Rendering an Element Outside the Loop based on Taxonomy Term in Bricks

Conditionally Rendering an Element Outside the Loop based on Taxonomy Term in Bricks

A user asks: Hiding element based on taxonomy I have an element in a footer that I want to hide if a page has a…
Categories: