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

Leave the first comment

 

Related Tutorials..

Pro
Checking ‘PublishPress Future’ plugin’s Future Action enabled posts

Checking ‘PublishPress Future’ plugin’s Future Action enabled posts

PublishPress Future is a handy plugin for scheduling actions like unpublishing or deleting posts (of any post type) at a specified date and time in…
Categories:
Pro
Query Variables Condition in Bricks

Query Variables Condition in Bricks

This Pro tutorial provides the steps to register a custom function that returns true or false depending on whether the current page URL has specific…
Render an element having a specific HTML ID based on value of a custom field

Render an element having a specific HTML ID based on value of a custom field

This tutorial provides the steps to use Bricks' bricks/element/render filter to conditionally output an element that has the specified HTML ID based on the value…
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
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:
Condition for Checking Current Post Category in Bricks

Condition for Checking Current Post Category in Bricks

How we conditionally display an element based on the current post's categories.
Categories:
Pro
User Role Condition in Bricks

User Role Condition in Bricks

This Pro tutorial provides the steps to conditionally output elements depending on the currently logged-in user's role. Step 1 Create a section Template having the…
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: