
Hi,
i have a loop for custom post type and i want to get a field from other post connected with relationship but due to that main loop is wp-query i can’t get this field using loops from relationship page documentation with or without setup_postdata.
Is there a way to make it working?
<?php
$args = array( 'post_type' => 'producenci', 'posts_per_page' => -1 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>
<div class="producentWrap">
<div class="producentImg" <?php if(get_field('image')){
$attachment_bg = get_field('image');
$sizebg = "product"; // (thumbnail, medium, large, full or custom size)
$imagebg = wp_get_attachment_image_src( $attachment_bg, $sizebg );?>style="background-image: url(<?php echo $imagebg[0]; ?>);"<?php }?>> </div>
<div class="producentTxt">
<h1>
<?php the_title(); ?>
</h1>
<?php
$posts = get_field('region');
if( $posts ): ?>
<?php foreach( $posts as $p ): // variable must NOT be called $post (IMPORTANT) ?>
/<?php the_title(); ?>/
<?php endforeach; ?>
<?php endif; ?>
<?php the_content(); ?>
<?php if(get_field('url')){; ?>
<a href="http://<?php the_field('url'); ?>" target="_blank"><?php the_field('url'); ?></a>
<?php }?>
</div>
</div>
<?php endwhile; ?>
thanks
DR
I’m assuming you’re talking about this section
$posts = get_field('region');
if( $posts ):
foreach( $posts as $p ): // variable must NOT be called $post (IMPORTANT) ?>
/<?php echo get_the_title($p->ID); ?>/
<?php
endforeach;
endif;
yes, i’ve tried both examples from documentation but i guess they work only in if (have_posts()) : while (have_posts()) : the_post();
loop
a relationship field can be set up to return post objects. The field should be returning a list of posts and you can access the values of those posts without have_posts. Each post in the returned array will have, in your case, $p->ID, which is the ID of the post. Using that ID you can get the value of an ACF field or other information about that post without the need of a have_posts() loop.