Home Forums Front-end Issues Problem returning post object data


Problem returning post object data

  • I have a Post Object field type (instructor) that filters by custom post type (person) and taxonomy (faculty, lecturer). Return format is set to Post Object, and is set to allow multiple values (although I’m not currently doing anything that would return more than one entry).

    I would like to return the title and permalink into a template page (specifically, the name of the instructor and a link to their page), but something is going wrong. If I try to print out the array using this:

    echo '<pre>';
        print_r( get_field('instructor')  );
    echo '</pre>';

    I get an array of content for an unrelated post.

    I’m sure my mistake is fairly basic, but I’m at my wit’s end.

  • Hi @colinf

    Were you trying to get the value outside of The Loop? Could you please try to debug it like this:

    echo '<pre>';
        print_r( get_field('instructor', 99)  );
    echo '</pre>';

    Where ’99’ is the post ID where the custom field is assigned. If it shows the right data, then you need to pass the ID or put the code inside The Loop.

    If that doesn’t help, could you please share the JSON or XML export file of your field group so I can test it out on my end?

    Thanks 🙂

  • Yes, I’m using this inside of a loop. Adding a proper post id returns the right result.

    Now how would I get the proper post id for each item in the loop and return the title and link? I assume it would look something like:

    <?php $post_object = get_field('instructor', $post->ID); ?>
    <p><a href="<?php echo get_permalink($post_object->ID); ?>"><?php echo $post_object->post_title; ?></a></p>

    … but that doesn’t work.

  • Hi @colinf

    That’s weird. If you put it inside The Loop, the returned value should be right. Did you add the code in an included file? If that’s the case, then you need to get the global $post variable first like this:

    global $post;
    $post_object = get_field('instructor', $post->ID);

    If that’s not the case, then could you please share the full template code you have? You can copy a long code to

    Thanks 🙂

  • Here is an example of useage in a loop. It’s not particularly long so I’m just pasting it here. Relevant code is on line 64, JSON is attached.

    Template Name: Current Semester
    $semester = get_field('current_semester', 'option');
    	<div class="content width" role="main">
    				<div class="page">
    				<?php get_sidebar('courses'); ?>
    		<section class="content-container">
    			<div id="regular-content">	
    				<!-- Undergraduate -->
    			$ud_args = array(
    							'post_type' => 'courses',
    							'taxonomy' => 'courses-category',
    							'field' => 'term_id',
    							'terms' => $semester,
    							'meta_query' => array(
    										'key' => 'course_number',
    										'value' => array( 100, 199 ),
    										'type' => 'numeric',
    										'compare' => 'BETWEEN'
    							'meta_key' => 'course_number',
    							'orderby' => 'meta_value_num',
       						'order' => 'ASC',
    							'posts_per_page' => '60' 
    				$ud_query = new WP_Query($ud_args);
    				if($ud_query->have_posts()) : ?>
    <h2 class="course-level-header">Undergraduate</h2>
    		<div class="courselist_content">
    				<?php  while($ud_query->have_posts()) : 
    		     					$ud_query->the_post(); ?>
    							<article id="post-<?php the_ID(); ?>" class="group course-list-wrapper">
    						<header class="index-header">
    							<h1 class="course-title">
    			<a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'thales' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark">	
    				<?php the_field('language_prefix'); ?> <?php the_field('course_number_prefix'); ?>  <?php the_field('course_number'); ?><?php the_field('course_number_suffix'); ?>: <?php the_title(); ?>
    						</header><!-- .entry-header -->
    						<div class="course-content">
    								<p class="section-spacing">
    								<?php the_field('timeframe'); ?> <span class="course-details"><?php the_field ('location'); ?></span>
    								<span class="course-details"><b>Instructor: <?php $post_object = get_field('instructor', $post->ID); ?> <a href="<?php echo get_permalink($post_object->ID); ?>"><?php echo $post_object->post_title; ?></a></p>
    <?php the_content(__(' &raquo;  read more  &raquo;'));?>
    <p class="section-spacing">Course Catalog Number: <?php the_field ('ccn'); ?></p>
    						</div><!-- .course-content -->
    			</article><!-- #post-<?php the_ID(); ?> -->
    				<?php endwhile; ?>
    			<?php endif; ?>		
    		<?php wp_reset_query(); ?> 
    		</section><!-- #content-container -->
    <?php get_footer(); ?>
  • Hi @colinf

    Could you please print_r() the get_the_ID() function like this:


    Does it show the right ID? If it’s, then I think you can pass it to the get_field() function like this:

    $post_object = get_field('instructor', get_the_ID());

    Thanks 🙂

  • That prints the right ID, but using get_the_ID isn’t returning the right result.

  • Hi @colinf

    That’s sure weird. Could you please open a new ticket and provide temporary admin credentials to your site? You can open a new ticket here: Also, please don’t forget to explain the issue again and provide the link to this thread.

    Thanks 🙂

  • Will do first thing in the morning. Cheers.

  • I have the same problem here.
    What is wrong?


    $currentpage = get_the_ID();
    //$post_objects = get_field('related_posts', get_the_id());
    $post_objects = get_field('related_posts', $currentpage);


    $post_objects = get_field('related_posts', 1024);

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

The topic ‘Problem returning post object data’ is closed to new replies.