Home › Forums › General Issues › Relationship field does not allow for post ID to be reset, help. › Reply To: Relationship field does not allow for post ID to be reset, help.
I found a work around, i suppose, but if you can tell me if this is the way i am supposed to use this field type, that would be helpful.
So to solve my problem, i wrapped every Relationship field query in a custom WP_QUERY loop. I pull the page/post id to load the current pages fields.
I’m thinking its better to use wp_query and not the wordpress loop. Doing it this way does seem to make it not ruin $post->ID for other sections of the page.
Here is a sample code from my page…i do the same thing in the header, page.php and sidebar.php – I’ve setup section on each page that will load a post that is assigned to it (I’m using to show banner ads in different spots of the page based on the settings the user makes. They can choose the banner ad per page).
<?php
$postid = get_the_ID();
$homepgqry = array(
'p' => $postid,
'post_type' => 'any',
'posts_per_page' => 1,
'status' => 'published'
);
$loop = new WP_Query( $homepgqry );
while ( $loop->have_posts() ) : $loop->the_post();
$posts = get_field('homepage_promo_bottom');
if( $posts ): ?>
<?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?>
<?php //setup_postdata($post); ?>
<?php
$attachment_id = get_field('promo_image');
$size = "promo-header";
$image_attributes = wp_get_attachment_image_src( $attachment_id, $size );
if( $image_attributes ) {
?>
<a href="<?php echo the_field('promo_url'); ?>" target="_blank"><img src="<?php echo $image_attributes[0]; ?>" width="<?php echo $image_attributes[1]; ?>" height="<?php echo $image_attributes[2]; ?>"></a>
<?php } endforeach; ?>
<?php // wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php endif; ?>
<?php endwhile; wp_reset_query(); ?>
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.