I'm working on a website for a band where you can add gigs and add the songs played on that specific gig.
So I've created two custom post types: - gig - song
I got a custom field "Songs" of type "Relationship". This field is shown on the Custom Post Type. This way I can add songs to a specific gig. This works perfectly.
But I want to show some statistics on the homepage of that website: I want to count how many times a specific song is played and show the top 10. So I guess that I have to loop over the gig custom post type and count the relation with 'songs'.
I thought this would do the trick:
<?php
$args = array(
'post_type' => 'gig'
);
?>
<?php $loop = new WP_Query($args); ?>
<?php if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php
print_r(get_field('songs'))
//$song_count = count(get_field('songs'));
//echo $song_count . " ";
the_title();
?><br />
<?php endwhile; ?>
<?php else: ?>
<!-- No gigs available -->
<?php endif; ?>
<?php wp_reset_postdata(); ?>
You can find the result of the print_r over here: http://snippi.com/s/njzg3uu
For example: the song "A memory" is on 2 gigs. That why you can find it twice in the array. The song "Wasted" can be found only once, because it's on 1 gig.