Hi talented people! 
How can I show the total count of posts related to a CPT via a Metabox Relationship?
My starting point is:
- Two CPTs on place: Products and Studies.
- One Metabox relationship with ID “products-studies”, relating from Products posts to Studies posts.
- Each Product post have several Studies posts related.
- I have a Product Template created in Bricks, and I am able to show all the related Studies perfectly.
Now, I need to just output the number of Studies related to each Product in the Product Template as a text string. Something like: “This product has X studies related”. Being X the number of related Studies via the Metabox Relationship.
Is this possible?
I have tried with {query_results_count} but it always results in 0, when it shouldn’t. So I am sure that’s not the right approach!
Any ideas?
Thank you in advance!!
Hey @knut_studio,
you’re on the right track… but you have to pass the Bricks ID of the element you’re using the query loop on to the {query_results_count}
tag. In my case: {query_results_count:syfswa}
.
Best,
André
Hey André!
Thanks for jumping in 
Not sure that I am following you.
I am working on the Product Template, but the related studies are not necessarily there. What I mean is that there’s no loop of related studies on the Product template. They are only shown on the Studies page, where there’s a list of all the studies with a filter by Product.
Now, on the Product Template I just need an indicator of the total number of studies. And then there’s a button that links to the Studies page, filtering the current product. So I just need to print the total number of studies related to that product on the product page, dynamically.
Is this possible?
Thanks!
Ah… misunderstood that, sorry.
In this case you could create a small helper function and use the {echo:...}
tag.
Function
function get_product_studies() {
$query = new WP_Query( [
'relationship' => [
'id' => 'product-studies',
'from' => get_the_ID(),
],
'nopaging' => true,
] );
return $query->post_count;
}
Template Usage
This product has {echo:get_product_studies} studies related
Best,
André
Hi André!
I am sure I miss explained it myself 
For some reason, the function is not working. It outputs nothing on the frontend, like “This product has studies related”, not 0, not anything, just a blank space.
The function I added to the site (the id is updated to the correct one):
That’s the relationship from Metabox:
I have tested the {echo:get_product_studies} added both in the Product Template and also in the Product “archive” page, where I have a query loop of product cards. It hasn’t worked in any case!
Any ideas? 
Sorry. I forgot to add that you have to whitelist the function as described here to make it work:
add_filter( 'bricks/code/echo_function_names', function( $function_name ) {
return [
'get_product_studies',
];
} );
Best,
André
Worked perfectly now, André!
Thank you very much!!
For future reference, this is the complete solution:
Function
function get_product_studies() {
$query = new WP_Query( [
'relationship' => [
'id' => 'product-studies',
'from' => get_the_ID(),
],
'nopaging' => true,
] );
return $query->post_count;
}
Whitelist the function
add_filter( 'bricks/code/echo_function_names', function( $function_name ) {
return [
'get_product_studies',
];
} );
Template Usage
This product has {echo:get_product_studies} studies related
Thanks again for your help, André! 
1 Like