Home › Forums › Front-end Issues › Displaying a relationship field on a custom post type taxonomy page › Reply To: Displaying a relationship field on a custom post type taxonomy page
I’m having the same exact problem you have but on my archive-custom-post-type.php page with Advanced Custom Fields Pro latest version.
This is the code i’m using:
<section id="debates-home">
<?php
$the_query = new WP_Query( array( 'post_type' => 'debate' , 'orderby' => 'date', 'posts_per_page' => 2 ) );
if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post();
$term = wp_get_post_terms($post->ID, 'area', array("fields" => "names"));
$term_slug = wp_get_post_terms($post->ID, 'area', array("fields" => "slugs"));
?>
<article>
<?php the_post_thumbnail(); ?>
<div class="info-debate">
<a class="category-link" href="<?php echo get_site_url(); ?>/area/<?php echo $term_slug[0] ?>/?post_type=debate"><?php echo $term[0]; ?></a>
<h3><a href="<?php the_permalink(); ?>" title="Ver debate: <?php the_title(); ?>"><?php the_title(); ?></a></h3>
<?php
$posts = get_field('columnas_del_debate');
if( $posts ): ?>
<ul>
<?php foreach( $posts as $post): ?>
<?php setup_postdata($post); ?>
<li>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_post_thumbnail('thumb'); ?></a>
<span class="tooltip">
<em><?php the_title(); ?></em><br> <?php the_field('autor_de_columna'); ?>
</span>
</li>
<?php endforeach; ?>
</ul>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
</div>
</article>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
</section>
The code works fine on my index.php and will break on archive-custom-post-type.php page. The only difference in the code on the archive page is that the loop is just the plain version with no args since I don’t need to modify the loop.
If turn wp_debug on i’ll see the following error on my index.php:
Notice: Trying to get property of non-object in /wp-includes/query.php on line 4618
Notice: Trying to get property of non-object in /wp-includes/query.php on line 4620
Notice: Trying to get property of non-object in /wp-includes/query.php on line 4622
Notice: Trying to get property of non-object in /wp-includes/query.php on line 4623
Notice: Trying to get property of non-object in /wp-includes/query.php on line 4631
Notice: Trying to get property of non-object in /wp-includes/query.php on line 4646
I tried what Elliot said before and ACF seems to be working fine cause the array with the related posts appears within the print_r.
Is there a way to use setup_postdata inside the loop on an archive-custom-post-type.php page?
Thanks!
PD: I lost my old forum account, dont know why? Was the forum reset or something?
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.