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

1 comment

  • Mounika

    Amazing, I tried this and it's working.

    Thank you.

Leave your comment

 

Related Tutorials..

Color Custom Field as Section Background Color on Category/Term Archives in Bricks

Color Custom Field as Section Background Color on Category/Term Archives in Bricks

How to use a color from a taxonomy term custom field for as the hero section background color on archive pages.
Categories:
Tags:
Pro
ACF Post Object Query Loop inside a Posts Query Loop in Bricks

ACF Post Object Query Loop inside a Posts Query Loop in Bricks

How to loop through a CPT and within each post, output a list of related posts.
Categories:
Pro
ACF Image Data in Bricks

ACF Image Data in Bricks

Displaying Alt text, Caption, Title and URL of an ACF image field for posts in a query loop.
Categories:
Tags:
Pro
Sub Field Value from the Last ACF Repeater Row in Bricks

Sub Field Value from the Last ACF Repeater Row in Bricks

Getting the value of a specific sub field of an ACF Pro‘s Repeater field and output in the query loop.
Categories:
Tags:
ACF Checkbox Items with Custom SVG Bullets in Bricks

ACF Checkbox Items with Custom SVG Bullets in Bricks

We walk through outputting list items with SVG icons based on the choice of a ACF Checkbox field.
Categories:
Tags:
Pro
ACF Relationship: Show all other events related to the artists of the current event

ACF Relationship: Show all other events related to the artists of the current event

How all other events related to the artists of the current event can be output in a Bricks query loop.
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…