13th Dec '24
/
1 comment

ACF Relationship: Count of Related Posts

Looking to get the number of CPT B posts related to a CPT A post when using a Relationship field with ACF?

Here’s a custom function for that:

/**
 * Get the count of related posts for the current post
 * or a specific post ID
 *
 * @param string $field_name The relationship field name slug.
 * @param int $post_id Optional. The ID of the post. Defaults to current post ID.
 * @return int Number of related posts
 */
function bl_get_related_posts_count( string $field_name, int $post_id = 0 ): int {
  $post_id = $post_id ?: get_the_ID();

  $related_posts = get_field( $field_name, $post_id );
  
  if ( ! $related_posts || ! is_array( $related_posts ) ) {
	return 0;
  }
  
  return count( $related_posts );
}

bl_get_related_posts_count( 'related_cities' ), for example, can be used to output the number of cities for each country in a country query loop where related_cities is the Relationship field name.

To use this in Bricks with echo dynamic data tag, whitelist it:

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

and then

{echo:bl_get_related_posts_count(related_cities)}

Get access to all 630 Bricks code tutorials with BricksLabs Pro

1 comment

  • Mounika

    Amazing, I tried this and it's working.

    Thank you.

Leave your comment

 

Related Tutorials..

Pro
Custom Dynamic Data Tags for ACF Field Label, Prefix and Suffix in Bricks

Custom Dynamic Data Tags for ACF Field Label, Prefix and Suffix in Bricks

Bricks lacks built-in dynamic tags for ACF field labels, prefixes, and suffixes. This tutorial shows how to add them using custom dynamic tags.
Categories:
Pro
ACF Checkbox Sub Field as Unordered List in Bricks

ACF Checkbox Sub Field as Unordered List in Bricks

Updated on 22 Sep 2023 This Pro tutorial shows how the checked options of a Checkbox-type of Sub Field inside a ACF Repeater field can…
Categories:
How to create a dynamic infinite logo slider with ACF and Bricks

How to create a dynamic infinite logo slider with ACF and Bricks

This tutorial provides the builder settings, PHP & CSS codes that can be pasted in order to create an infinite logo slider in pure CSS…
Pro
Frontend Editing with ACF in Bricks

Frontend Editing with ACF in Bricks

This Pro tutorial provides the steps to show a form on single posts on the front end for admins and the post author using which…
Categories:
Pro
Configuring Image Gallery dynamic source in Bricks

Configuring Image Gallery dynamic source in Bricks

Let's say you have a custom field created with ACF Pro of the type Gallery called "Property Gallery" for posts of "Property" CPT. In the…
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:
Pro
Conditional Output Based on ACF Date Field in Bricks

Conditional Output Based on ACF Date Field in Bricks

Updated on 1 Aug 2024 Looking to have a Section or other elements in Bricks rendered on the frontend only if today's date is before…
Categories: