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
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…
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 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…
Conditionally Outputting Elements only for Posts that have a Specific Taxonomy Term

Conditionally Outputting Elements only for Posts that have a Specific Taxonomy Term

Using the core WordPress has_term() function for checking if the current post has a specific term of a given taxonomy.
Categories:
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:
Pro
Conditional Output in Bricks based on if Content has Headings

Conditional Output in Bricks based on if Content has Headings

How to prevent the output of the table of contents if the content has no headings.
Categories:
Tags:
Pro
Conditional Rendering in Bricks Based on CSS Class

Conditional Rendering in Bricks Based on CSS Class

Updated on 19 Jun 2024 A user asks: Is there a way to apply the same conditions to several elements like we set styles appending…
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: