10th Apr '25
/
0 comments

Check if a given category has at least one post

There could be situations where you want to check if a given category (by its slug) has at least 1 post assigned to that category.

This tutorial shows how we can define a custom function for this and use it with Bricks‘ dynamic data condition.

After implementing this tutorial you should be able to do something like

where current-exhibition is the slug of the category. With that in place, the Section will not be output if there is no post assigned to the Current Exhibition category.

Step 1

Add the following in child theme‘s functions.php (w/o the opening PHP tag) or a code snippets plugin:

<?php

/**
 * Check if a category has at least one published post.
 *
 * @param string $category_slug The slug of the category to check.
 * @return bool True if the category has at least one published post, false otherwise.
 */
function bl_category_has_posts( string $category_slug ): bool {
  // Get the category object by slug
  $category = get_term_by( 'slug', $category_slug, 'category' );
  
  // If category doesn't exist, return false
  if ( ! $category || is_wp_error( $category ) ) return false;
  
  // The count property contains the number of published posts in this category
  return $category->count > 0;
}

Step 2

Whitelist the bl_category_has_posts function.

Ex.:

<?php 

add_filter( 'bricks/code/echo_function_names', function() {
  return [
    'bl_category_has_posts'
  ];
} );

You should also add other functions (native or custom) being used in your Bricks instance besides bl_category_has_posts. This can be checked at Bricks → Settings → Custom code by clicking the Code review button.

More info on whitelisting can be found here.

Step 3

Add a dynamic data condition on any element in the Bricks builder which should be output if a specific category contains a post.

{echo:bl_category_has_posts(current-exhibition)}

Replace current-exhibition with the slug of category you want to check for.

Get access to all 610 Bricks code tutorials with BricksLabs Pro

Leave the first comment

 

Related Tutorials..

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…
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:
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…
Categories:
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:
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:
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:
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
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: