Support

Account

Home Forums General Issues Display fields from two post types in one loop/template

Solved

Display fields from two post types in one loop/template

  • I have two post types. I need to display content from each within the same loop. At the moment, the below line breaks the page with a ‘Catchable fatal error: Object of class WP_Post could not be converted to string error’.

    <?php the_field('release_artist_name'); ?>

    How to I get the release_artist_name field to stop breaking the site? It is a field via a Post Object (post type is named ‘artists’). Any help appreciated.

    <?php 
       // get posts
       $posts = get_posts(array(
       	'post_type'			=> 'releases',
       	'posts_per_page'		=> 36,
       'meta_key'			=> 'release_date',
       	'orderby'			=> 'meta_value_num',
       	'order'				=> 'DESC',
       'paged' 			=> ( get_query_var('paged') ? get_query_var('paged') : 1 )
       ));
       
       if( $posts ): ?>
    <?php foreach( $posts as $post ): 
       setup_postdata( $post )
       
       ?>
    <?php 
       $image = get_field('release_cover_art');
       if( !empty($image) ): 
       	// thumbnail
       	$size = 'medium';
       	$thumb = $image['sizes'][ $size ];
       ?>
    <img src="<?php echo $thumb; ?>" alt="<?php echo $alt; ?>" class="img-responsive" />
    <?php the_field('release_artist_name'); ?>
    <?php endif; ?>
    <?php endforeach; ?>
    <?php wp_reset_postdata(); ?>
    <?php endif; ?>
  • Hi @lowercase

    The_field() functions is used to echoes the returned value. Since the post object field returns WP_Post object, you’ll get that error because PHP doesn’t allow you to echo an object. Please take a look at this page to learn more how to show a post object field: https://www.advancedcustomfields.com/resources/post-object/.

    I hope this makes sense.

  • Thanks James. It didn’t make sense 2 hours ago, but looks to be clearer now. I have it working at last using some tweaked code from your link.

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Display fields from two post types in one loop/template’ is closed to new replies.