Get the title of custom post type in another loop

I am in the single-product.php loop and the product has a relationship to an artist post type to which I have the id off the artist.

I need to get the_title() from my custom artist post type while in the single-product.php loop.

My code is getting the title of the product rather than the artist. My code is below. Can anyone please help?

    $artistId = get_field('artist');

    $postId = get_post($artistId);
    if ( $postId ):
        setup_postdata($postId);
        ?>
        <span id="chty_17">
            <dt><?php the_title(); ?></dt>
        </span>
        <?php
        wp_reset_postdata();
    endif;

1 Answer
1

You can pass a post ID to get_the_title(). So instead of using the_title() to display it, fetch it first like this.

$artist_title = get_the_title( $artistID );
echo $artist_title;

You could do that in one line of course but you might need it somewhere else, too.

Leave a Comment