WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. transmitstudio
    Member
    Posted 9 months 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>
    				<address>
    					<?php the_field('facility_address'); ?>,
    					<?php the_field('facility_city'); ?> OH
    					<?php the_field('facility_zip'); ?> •
    					<?php the_field('program_main_phone'); ?>
    				</address>
    			<?php endwhile; ?>
    			</div>
    		<?php
    		// Prevent weirdness
    		wp_reset_postdata();
    	endif;

    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?

    http://wordpress.org/plugins/posts-to-posts/

  2. hiphopinenglish
    Member
    Posted 6 months 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.

Reply

You must log in to post.

About this Plugin

About this Topic