E
E
Eugene Zhadko2021-08-01 18:06:27
Taxonomy
Eugene Zhadko, 2021-08-01 18:06:27

How to get the number of records from the Relationship field (ACF)?

There are custom record types "Stores" and "Shopping centers".
Through the plugin ACF (Advanced Custom Fields), a field of type "Records" was created for "Stores", to indicate whether the store belongs to a shopping center. It is necessary to display the total number of stores referring to a specific (current) mall.

There is a code that doesn't work

<?php $args = array( 'post_type' => 'magaziny' ); ?>
<?php $loop = new WP_Query($args); ?>
<?php if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php 
$song_count = count(get_field('center', get_the_ID())); ?>
<?php endwhile; ?>
<a href="<?php echo $post_href; ?>" class="item-btn">
<?php echo $song_count . ' магазинов</a>'; ?></a>
<?php else: ?>
<?php endif; ?>
<?php wp_reset_postdata(); ?>


A similar question was already asked here, a few years ago, but I did not find a working solution to this issue. I will be grateful for your help!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Aricus, 2021-08-02
@Aricus

In theory, it should work. Here is a piece of working code from my project:

$solutions = get_field('solution', get_the_ID());
foreach ($solutions as $solution): ?>
  <div class="device_menu_block_elem"><a href="<?=$solution->guid?>" class="device_menu_link"><?=$solution->post_title?></a></div>
<?php endforeach;

See what you get in I think the results will make it clear what the problem is.
var_dump(get_field('center', get_the_ID()));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question