Answer the question
In order to leave comments, you need to log in
How to get number of records from Relationship field (ACF)?
On the WordPress site:
- Created a custom post type “producers” .
- Created custom post type “movies” .
- To be able to link records of the “movies” type with records of the “producers” type, a custom field “movies_of_this_producer” has been created (a custom field of the Relationship field type created using the Advanced Custom Fields plugin www.advancedcustomfields.com/resources/relationship ).
Accordingly, the custom field “movies_of_this_producer” (a custom field of the Relationship field type) is displayed on the page for creating/editing records of the arbitrary type “movies”, in this custom field for a specific record of the “movies” type it is possible to specify one or more records of the “producers” type (then -there is a "relationship" of a specific "movies" type entry with one or more "producers" type entries).
As a result, the site has a correct relationship between records of type “movies” and records of type “producers”.
For example, there is a record “Steven Spielberg” (a record of an arbitrary type “producers”) and it is associated with 5 records using a custom field “movies_of_this_producer” (a custom field of type Relationship field): “Jaws” , “Jurassic Park”, “Schindler's List” , “The Terminal” , “Bridge of Spies” (with 5 entries of arbitrary type “movies”).
Now, for each specific record of the arbitrary type “producers”, you need to get a number - the number of records of the arbitrary type “movies” associated with this particular record of the arbitrary type “producers” (connected using the custom field of the type Relationship field “movies_of_this_producer”).
*That is, in the example with the entry “Steven Spielberg” (an entry of the arbitrary type “producers”) - the required number will be: 5 , since, based on the example, exactly 5 entries of the arbitrary type “movies” are associated with the entry “Steven Spielberg” .
Question:How to get for each record of the custom type "producers" - the number of records of the custom type "movies" associated with this particular record (linked using a custom Relationship field (ACF)? What should be the code for this?
Answer the question
In order to leave comments, you need to log in
I have not played with "ACF", but everyone has the same logic, the easiest way is to make a loop in a loop:
<?php
$producers = get_posts(array(
'numberposts' => -1,
'post_type' => 'producers',
));
if($producers) {
foreach($producers as $item) {
$producer_id = $item->ID;
$producer_name = $item->name;
$producer_movies_count = get_producer_movies_count ($producer_id);
echo $producer_name . ' – ' . $producer_movies_count . '<br>';
}
}
function get_producer_movies_count ($producer_id) {
$movies = get_posts(array(
'numberposts' => -1,
'post_type' => 'movies',
'meta_key' => 'movies_of_this_producer',
'meta_value' => $producer_id
));
$count = count($movies);
return $count;
}
?>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question