How can i count the post added in relationship field. The relationship field is a plugin (http://www.advancedcustomfields.com/docs/field-types/relationship/)

In my admin i have a MetaBox i create to link the post from another post type to my posts. please see picture below in the link


Subtitle and Trailers

How can i get the number of post attach to the relationship field.

Ex: If the relationship field has 4 articles added in site in tabs i want to be able to get the number of articles

Ex: Trailers(1) - that are display in Front-end.

Something like this ( cinemagia.ro/filme/avatar-2-578279/ )

To retrive the post and display into my theme i use

<?php if(get_field('trailere_filme') != ""){foreach(get_field('trailere_filme') as $post_object): ?>        
<div class="box-trailere">
<div class="trailer-img">
<a href="<?php echo get_permalink($post_object->ID); ?>" rel="bookmark" title="<?php printf(__('Trailer film %s', 'kubrick'), the_title_attribute('echo=0')); ?>">
<span class="tip"><?php echo get_field( 'tip', $post_object->ID ); ?></span>
<span class="trai-hov"></span>
<img src="http://img.youtube.com/vi/<?php echo get_field( 'trailer', $post_object->ID ); ?>/<?php echo get_field( 'img_trailer', $post_object->ID ); ?>" alt="Trailer <?php echo get_the_title($post_object->ID) ?>" title="Trailer <?php echo get_the_title($post_object->ID) ?>"></a>
<span class="duration"><?php echo get_field( 'time', $post_object->ID ); ?></span>
<span class="trailer-title"><a href="<?php echo get_permalink($post_object->ID); ?>" title="Trailer <?php echo get_the_title($post_object->ID) ?>"><?php echo get_the_title($post_object->ID) ?></a></span>
<?php endforeach;}else{?>
<div class="no-trailer">Acest film nu are trailer.</div>
<?php } ?>

And for tags related to post

in my hteme i use:

$orig_post = $post;
        global $post;
        $tags = wp_get_post_tags($post->ID);

        if ($tags) {
            $tag_ids = array();
            foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
                'tag__in' => $tag_ids,
                'post__not_in' => array($post->ID),
                'post_type'=> 'stiri',
                'posts_per_page'=>1, // Number of related videos that will be shown.

$recentPosts = new WP_Query($args);
<?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
<div class="sidebar-titlu"><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></div>
<div class="sidebar-stiri">
<img src="/scripts/timthumb.php?src=<?php the_field('imagine_stire'); ?>&h=60&w=90&zc=1" alt="" title="<?php the_title(); ?>" />
<?php $excerpt = get_the_excerpt();echo string_limit_words($excerpt,30);?><a class="detalii" href="<?php the_permalink() ?>" rel="bookmark" title="<?php printf(__('Permanent Link to %s', 'kubrick'), the_title_attribute('echo=0')); ?>">...detalii</a>
<?php endwhile; ?>


  • Dear Batman, what is a relationship field? Where does it comes from? Where do you want to get its count, frontend, backend? Without context, it is impossible to answer...
    – brasofilo
    Jan 6, 2013 at 19:51
  • Hy brasofilo, thanks for answer, i use advance custom field as a plugin, i want to display the count in frontend
    – Batman
    Jan 6, 2013 at 20:05
  • if you ar kind i want to display te count for the post that are retrived by tags..too
    – Batman
    Jan 6, 2013 at 20:06
  • 1
    Please, Copil, please, improve the Questions that you ask when someone makes a comment that helps to clarify the Q. There's a link over here: edit, that you can use to edit the Q, add more information to it, so people can revert the downvote when you show more research efforts and care for the Q. While you are at it, read the documentation about the Relationship Field, apply it to your problem and modify the Q accordingly. Thanks!
    – brasofilo
    Jan 6, 2013 at 20:17

1 Answer 1


The relationship field will return an array of post objects, so just count them:

$related_posts = get_field( 'trailere_filme' );
echo count( $related_posts );
  • hy milo can you tell me how to retrive the count for the tags... Ex: the articles for that movie...
    – Batman
    Jan 6, 2013 at 21:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.