Hello,
I have a CPT called ‘videos’ and another CPT called ‘instructors’.
I have created a Post Object custom field, called ‘instructor’.
On the instructorS single page i want to query a list of videos that have that instructor assigned to them.
This is my code so far:
<?php
$video_id = $post->ID;
$posts = get_posts(array(
‘posts_per_page’ => -1,
‘post_type’ => ‘videos’
));
?>
<?php if( $posts ): ?>
<?php foreach( $posts as $post ):
setup_postdata( $post );
?>
<?php
$post_objects = get_field(‘instructor’);
if( $post_objects ): ?>
<?php foreach( $post_objects as $post_object): ?>
<?php $instructor_id = $post_object->ID;?>
<?php endforeach; ?>
<?php endif;?>
<?php if($instructor_id == $video_id) :?>
<div class=”col-xs-6 col-md-4 curs_instructor”>
” title=”<?php the_title(); ?>”>
<div class=”instructor_video_slide”>
<?php the_post_thumbnail(‘instructor-thumb’, array(‘class’ => ‘aligncenter instructor_img’)); ?>
<div class=”instructor_video_slide_title”><?php the_title();?></div>
</div>
</div>
<?php endif;?>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
It works ok, the only issue is that if a video has more that 1 instructors assigned, it will only show 1, instead of all.
Can you please help me?

You need to put the code that’s displaying the instructor inside of the foreach loop. I’d post modified code but you’ll need to use the code tags available in this forum so that your code is usable first.
Hello,
I used the same code as viorelepuran above, but unfortunately, I get nothing in this line of code:
$instructor_id = $post_object->ID;
When I use var_dump($post_objects);, everything seems to be OK, but when i watch $post_object, I cannot see any keys like ID, post_title and so on.
Could you please tell me, where is the problem?
Thank you!
The topic ‘Query CPT thats has Post Object Assigned’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.