T
T
tbalero2015-11-29 01:33:52
WordPress
tbalero, 2015-11-29 01:33:52

How do I get the number of records from the Relationship field (ACF) for records that have a "reverse" relationship?

On the WordPress site:
- Created 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”.
- *Records of the “movies” type have a direct connection with the records of the “producers” type (since the custom field “movies_of_this_producer” is displayed on the pages for creating/editing the records of the “movies” type);
- *For records of type “producers” - reverse ("reverse"
For example, there is an entry “Steven Spielberg” (an entry of arbitrary type “producers”) and it is associated with 5 entries using the 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 random “movies” entries).
In order to display a list of related records of arbitrary type “movies” on the page of a specific record of the arbitrary type “producers” (template single-producers.php), I use a reverse query ("Reverse Query"), as in this manual: www.advancedcustomfields. com/resources/tutorials/q... . And it works correctly.
1) Now you need to display on the main page (index.php template) a list of records of arbitrary type “movies”, and for each specific record of arbitrary type “movies” displayed in this list, you need to display the number of records of arbitrary type “producers” associated with this particular record (associated with a custom "movies_of_this_producer relationship" field of type Relationship field).
2) And you also need to display on the main page (index.php template) a list of records of the arbitrary type “producers”, and for each specific record of the arbitrary type “producers” displayed in this list, you need to display the number of records of the arbitrary type “movies”, associated with this particular post (associated with a custom "movies_of_this_producer relationship" field of type Relationship field).
- To solve task No. 1 (display on the main page a list of records of an arbitrary type “movies” with a figure for the number of related records of an arbitrary type “producers”), I use this code (and it works 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 a similar code to solve problem #2 (display on the main page a list of posts of a custom type “producers” with a figure for the number of related posts of a custom type “movies”) - this code does not work correctly (as a result I get a list of posts of an arbitrary type “producers”, but at the same time, the figure for the number of related records of an arbitrary type “movies” is erroneously equal to zero). Here's the code (which I'm trying to use but doesn't work correctly):
<?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 - perhaps it is necessary to use some kind of "reverse" method, because records of arbitrary type "producers" - have a "reverse" ("reverse") relationship with records of arbitrary type "movies" (relationship via custom field "movies_of_this_producer" of type Relationship field), but I don't know how to do it.
Question: How to display on the main page (index.php template) a list of records of the custom type “producers”, with the figure of the number of related records of an arbitrary type “movies” (linked using the "movies_of_this_producer" field of the Relationship field type)? (* this is task #2 described in this question)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pavel Chesnokov, 2015-12-01
@cesnokov

<?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
                    $producers_movies = get_posts(array(
                      'post_type' => 'movies',
                      'meta_query' => array(
                        array(
                          'key' => 'movies_of_this_producer',
                          'value' => '"' . get_the_ID() . '"',
                          'compare' => 'LIKE'
                        )
                      )
                    ));
                    if ($producers_movies) {
                      echo count($producers_movies);
                    } esle {
                      echo '0';
                    }
                    ?>

                </li>
            <?php
            endwhile;
            ?>
        </ul>
    </div>
</div>
<?php endif; ?>

<?php
wp_reset_postdata();
?>

Instead of this:
<?php
echo $producer_count = count(get_field('movies_of_this_producer'));
?>

This:
<?php
$producers_movies = get_posts(array(
   'post_type' => 'movies',
   'meta_query' => array(
      array(
         'key' => 'movies_of_this_producer',
         'value' => '"' . get_the_ID() . '"',
         'compare' => 'LIKE'
      )
   )
));
if ($producers_movies) {
   echo count($producers_movies);
} esle {
   echo '0';
}
?>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question