Support

Account

Home Forums ACF PRO How to get the number of posts from Relationship field – for "reverse" posts?

Helping

How to get the number of posts from Relationship field – for "reverse" posts?

  • – I have custom post type “producers”.

    – And i have custom post type “movies”.

    – I have relationship field “movies_of_this_producer”.

    On the page of edit/creation new post of custom post type “movies”

    *I have this relationship field “movies_of_this_producer” on the page of edit/creation new post of custom post type “movies”.

    So, to display a list of related posts of custom post type “movies” on a single post page (of custom post type “producers”) (single-produsers.php template) – I use Reverse Query (like in this tutorial: http://www.advancedcustomfields.com/resources/tutorials/querying-relationship-fields/). And it’s work correctly.

    1) Now I need to display on the main page (index.php template) list of posts of custom post type “movies”, with number of related “producers” posts (related via relationship field “movies_of_this_producer”).

    2) And now I need to display on the main page (index.php template) list of posts of custom post type “producers”, with number of related “movies” posts (related via relationship field “movies_of_this_producer”).

    – To solve the problem number1 (display on the main page list of posts of custom post type “movies”, with number of related “producers” posts), I use this code (and it’s work correctly):

    <?php
    $TESTrel_movies = new WP_Query( array(
        'post_type' => 'movies',
        'post__not_in' => array($post->ID),
        'posts_per_page' => 5,
        'orderby' => 'meta_value_num',
        'order' => 'ASC',
        'meta_key' => 'movies_of_this_producer',
        'offset' => 0,
        ) );
    ?>
    
    <?php if( $TESTrel_movies ): ?>
    <div class="content_block_wrap two_columns_right col-md-6">
        <div class="content_block transparent_block_with_border">
            <div class="block_title">
                Title here
            </div>
            <ul>
                <?php
                    while ( $TESTrel_movies->have_posts() ) :
                $TESTrel_movies->the_post();
                ?>
                    <li class="content_item col-xs-12">
                        <a class="post_thumbnail" href="<?php echo get_permalink( $TESTrel_movies->ID ); ?>">
                            <?php echo get_the_post_thumbnail( $TESTrel_movies->ID, '90x60-thumb' ); ?>
                        </a>
    
                        <a href="<?php echo get_permalink( ); ?>"><?php echo get_the_title( ); ?></a>
                        
                        Number of related posts: <?php
                        echo $movie_count = count(get_field('movies_of_this_producer'));
                        ?>
    
                    </li>
                <?php
                endwhile;
                ?>
            </ul>
        </div>
    </div>
    <?php endif; ?>
    
    <?php
    wp_reset_postdata();
    ?>

    – However, if I use analogical code to solve the problem number2 (display on the main page list of posts of custom post type “producers”, with number of related “movies” posts) – it’s work incorrectly (as a result I get a list of “producers” posts, but the values of the number of related “movies” posts – equals zero). Here is this code (that I try to use, but it’s work incorrectly):

    <?php
    $TESTrel_producers = new WP_Query( array(
        'post_type' => 'producers',
        'post__not_in' => array($post->ID),
        'posts_per_page' => 5,
        'orderby' => 'meta_value_num',
        'order' => 'ASC',
        'meta_key' => 'movies_of_this_producer',
        'offset' => 0,
        ) );
    ?>
    
    <?php if( $TESTrel_producers ): ?>
    <div class="content_block_wrap two_columns_right col-md-6">
        <div class="content_block transparent_block_with_border">
            <div class="block_title">
                Title here
            </div>
            <ul>
                <?php
                    while ( $TESTrel_producers->have_posts() ) :
                $TESTrel_producers->the_post();
                ?>
                    <li class="content_item col-xs-12">
                        <a class="post_thumbnail" href="<?php echo get_permalink( $TESTrel_producers->ID ); ?>">
                            <?php echo get_the_post_thumbnail( $TESTrel_producers->ID, '90x60-thumb' ); ?>
                        </a>
    
                        <a href="<?php echo get_permalink( ); ?>"><?php echo get_the_title( ); ?></a>
                        
                        Number of related posts: <?php
                        echo $producer_count = count(get_field('movies_of_this_producer'));
                        ?>
    
                    </li>
                <?php
                endwhile;
                ?>
            </ul>
        </div>
    </div>
    <?php endif; ?>
    
    <?php
    wp_reset_postdata();
    ?>

    I think that in this case – may need to use some kind of “Reverse” way (because “producers” posts – have “reverse” relationship with “movies” posts via relationship field “movies_of_this_producer”), but I don’t know how to do it.

    Question: How to display on the main page (index.php template) list of posts of custom post type “producers”, with number of related “movies” posts (related via relationship field “movies_of_this_producer”)? (*it’s a problem number2, described in this topic)

  • Hi there,

    You can achieve this by looking into this page: http://www.advancedcustomfields.com/resources/querying-relationship-fields/. On the last code example, you can see that you need to query the posts using get_posts(). It will be something like this:

    <!-- TESTrel_producers -->    
    <?php
    $TESTrel_producers = new WP_Query( array(
        'post_type' => 'producers',
        'post__not_in' => array($post->ID),
        'posts_per_page' => 20,
        'orderby' => 'title', //change it if you want to order by something else
        'order' => 'ASC',
        'offset' => 0,
        ) );
    ?>
    
    <?php if( $TESTrel_producers ): ?>
    <div class="content_block_wrap two_columns_right col-md-6">
        <div class="content_block transparent_block_with_border">
            <div class="block_title">
                List of posts of custom post type “producers”, with number of related “movies” posts:
            </div>
            <ul>
                <?php
                    while ( $TESTrel_producers->have_posts() ) :
                $TESTrel_producers->the_post();
                ?>
    
                    <?php
                    $the_movies = get_posts(array(
                        'post_type' => array( 'movies' ), // uncomment it if you only want to get the result from movies post type
                        'posts_per_page' => -1,
                        'meta_query' => array(
                            array(
                                'key' => 'movies_of_this_producer', // name of custom field
                                'value' => '"' . get_the_ID() . '"', // matches exaclty "123", not just 123. This prevents a match for "1234"
                                'compare' => 'LIKE'
                            )
                        )
                    ));
                    ?>
    
                    <li class="content_item col-xs-12">
                        <a class="post_thumbnail" href="<?php echo get_permalink( $TESTrel_producers->ID ); ?>">
                            <?php echo get_the_post_thumbnail( $TESTrel_producers->ID, '90x60-thumb' ); ?>
                        </a>
    
                        <a href="<?php echo get_permalink( ); ?>"><?php echo get_the_title( ); ?></a>
                        
                        <span class="sm">Number of related posts:</span> <?php
                        echo $producer_count = count($the_movies);
                        ?>
    
                    </li>
                <?php
                endwhile;
                ?>
            </ul>
        </div>
    </div>
    <?php endif; ?>
    
    <?php
    wp_reset_postdata();
    ?>
    <!-- TESTrel_producers end -->

    I hope that helps.

Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘How to get the number of posts from Relationship field – for "reverse" posts?’ is closed to new replies.