I’m using this code to get the relationship fields but somehow it isnt working and i cant find the problem.
this is the code:
<?php
$ids = get_field('relacionado', false, false);
$query = new WP_Query(array(
'post_type' => 'produtos',
'posts_per_page' => 3,
'post__in' => $ids,
'post_status' => 'any',
'orderby' => 'rand',
));
foreach ($query->posts as $post) {
setup_postdata($post);
if(have_posts()){
the_post();
?>
<div>
<img src="<?php echo get_field('imagem_chamada'); ?>" alt="">
<h2><a href="<?php the_permalink() ?>" title="" rel="bookmark"><?php the_title() ?></a></h2>
<p><?php echo get_field('chamada'); ?></p>
</div>
<?php } } ?>
some help would be appreciated
Hi @ramon
Please check that your $ids array is correct by debugging it like so:
<?php echo '<pre>';
print_r( $ids );
echo '</pre>';
die; ?>
Looking at your code, it seems to be a jumble of WP_Query
and get_posts
.
Please keep to 1 consistent style like so:
<?php
$ids = get_field('relacionado', false, false);
$the_query = new WP_Query(array(
'post_type' => 'produtos',
'posts_per_page' => 3,
'post__in' => $ids,
'post_status' => 'any',
'orderby' => 'rand',
));
// The Loop
if ( $the_query->have_posts() ):
while ( $the_query->have_posts() ): $the_query->the_post();
?>
<div>
<img src="<?php echo get_field('imagem_chamada'); ?>" alt="">
<h2><a href="<?php the_permalink() ?>" title="" rel="bookmark"><?php the_title() ?></a></h2>
<p><?php echo get_field('chamada'); ?></p>
</div>
<?php
endwhile;
endif;
?>
It is possible that you don’t even need to use a custom WP_Query at all. Have you tried looping over the results from get_field('relacionado')
?
Thanks
E