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

1 comment

  • Mounika

    Amazing, I tried this and it's working.

    Thank you.

Leave a Reply to Mounika (Cancel Reply)

 

Related Tutorials..

Pro
ACF Flexible Content Field – Layout Labels and Sub Field Labels

ACF Flexible Content Field – Layout Labels and Sub Field Labels

How to output the text of layout labels and/or sub field labels using the Flexible Content field of ACF Pro.
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
Outputting ACF Repeater’s Sub Fields

Outputting ACF Repeater’s Sub Fields

This Pro tutorial provides the code for printing custom HTML after looping through rows of a ACF Repeater (available in ACF Pro) that has a…
Categories:
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:
Pro
Bricks Dynamic Data Tag for Text-type Custom Field Value with Word Limit

Bricks Dynamic Data Tag for Text-type Custom Field Value with Word Limit

How to register a new dynamic tag for setting excerpt word limits and outputting an ellipsis (...) at the end.
Categories:
How to populate a map with dynamic markers from a CPT using ACF in Bricks (PART 2)

How to populate a map with dynamic markers from a CPT using ACF in Bricks (PART 2)

This tutorial provides the PHP & JS codes that can be pasted in order to create a flying effect on map markers each time your…
Pro
Post-specific ACF Repeater Galleries as Sliders in Bricks

Post-specific ACF Repeater Galleries as Sliders in Bricks

How to output ACF Repeater field rows with the images of the Gallery-type sub field as a slider.