nik218
nik218

Reputation: 68

How to Show All Posts from Relationship Field

I have two custom post types: staff and locations. In the CPT for a staff member, I am choosing corresponding locations using the ACF relationship field.

On the front-end, I am displaying a location and listing all staff members from that location. My problem, however, is that I cannot get all the staff members to show. Only a max of 5 are showing.

I currently have one location with 7 staff assigned to it. Only 5 show on the front-end. If I delete one of the 5 that are showing, another one takes its place.

'''

                                <?php while ( have_posts() ) : the_post(); ?>

                                            <?php

                                            /*
                                            *  Query posts for a relationship value.
                                            *  This method uses the meta_query LIKE to match the string "123" to the database value a:1:{i:0;s:3:"123";} (serialized array)
                                            */

                                            $staff = get_posts(array(
                                                'post_type' => 'people',
                                                'meta_query' => array(
                                                    array(
                                                        'key' => 'location', // name of custom field
                                                        'value' => '"' . get_the_ID() . '"', // matches exactly "123", not just 123. This prevents a match for "1234"
                                                        'compare' => 'LIKE'
                                                    )
                                                )
                                            ));

                                            ?>
                                            <div class="people-grid">
                                            <?php if( $staff ): ?>
                                                <?php foreach( $staff as $doctor ): ?>
                                                    <?php

                                                    $photo = get_field('photo', $doctor->ID);

                                                    ?>
                                                    <div class="single-person">
                                                            <img src="<?php echo $photo['url']; ?>" alt="<?php echo $photo['alt']; ?>" />
                                                            <h3><?php echo get_the_title( $doctor->ID ); ?></h3>
                                                            <h4><?php echo get_field('position_title', $doctor->ID); ?></h3>

                                                    </div>
                                                <?php endforeach; ?>
                                            </div>
                                            <?php endif; ?>

                                <?php endwhile; // end of the loop. ?>'''

I expect there to be 7 members on this page, but I can only see 5: https://aptw.nk-creative.com/locations/goshen-ny/

Upvotes: 0

Views: 1944

Answers (1)

Daniel Vickers
Daniel Vickers

Reputation: 1074

It's probably listening to the default setting of post per page.

$staff = get_posts(array(
    'post_type' => 'people',
    'posts_per_page' => -1,
    'meta_query' => array(
        array(
            'key' => 'location', // name of custom field
            'value' => '"' . get_the_ID() . '"', // matches exactly "123", not just 123. This prevents a match for "1234"
            'compare' => 'LIKE'
        )
    )
));

Try changing your query to the above and it should overwrite your default to display all that are pulled in the query.

Upvotes: 2

Related Questions