Posts 2 Posts
Display custom field from parent post (2 posts)

  1. transmitstudio
    Posted 1 year ago #

    I need to display a custom field from the parent connected post. It's easier to describe through the code:

    $connected = new WP_Query( array(
    	  'connected_type' => 'programs_to_facilities',
    	  'connected_items' => get_queried_object(),
    	  'nopaging' => true,
    	) );
    	// Display connected facility
    	if ( $connected->have_posts() ) : ?>
    			<div class="facility">
    			<?php while ( $connected->have_posts() ) : $connected->the_post(); ?>
    				<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    					<?php the_field('facility_address'); ?>,
    					<?php the_field('facility_city'); ?> OH
    					<?php the_field('facility_zip'); ?> •
    					<?php the_field('program_main_phone'); ?>
    			<?php endwhile; ?>
    		// Prevent weirdness

    Where I have program_main_phone, that is from the parent connected post. In this case the Parent post is of post type 'program' and this code displays the connected 'facility' but I do need to display one field from the parent post. Is this possible?


  2. J M
    Posted 1 year ago #

    If you are showing this on a single template, get the custom field value you need OUTSIDE this loop:
    <?php $field = get_field($field_name, $post_id, $format_value); ?>
    Then echo $field; in the place in your template that it needs to be - inside the above loop if needs be.

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • Posts 2 Posts
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic