Support

Account

Home Forums General Issues Display custom field value from another post type

Solved

Display custom field value from another post type

  • Hi

    I have a Custom Post Type called Playlists and other called Projects

    I want to display the project_description field from CPT Projects into CPT Playlits if project_name field (Playlists) = post title (Projects)

    project_name = select field

    I trying make this, but not works:

    $project_name = get_field(“project_name”);
    the_field(‘project_description’, $name);

  • Is the field project_name a select field that you’ve added choices to or is it a post object or at relationship field?

  • Hi, John
    It’s a a select field that I’ve added choices
    But I do not know if this is the simplest way to do

  • I think using post object would be better

  • You are correct, you should use a post object field. While it may be possible to do a query based on something selected, it will not be easy to accomplish. A post object field saves the post ID that is related so you can use the post ID to easily get the information you need from the other post.

  • Thanks ! This is works:

    <?php
    
    $post_object = get_field('project_name');
    
    if( $post_object ): ?>
    
        <div>
        	<h4><?php echo get_the_title($post_object->ID); ?></h4>
            <span>Post Object Custom Field: <?php the_field('project_description', $post_object->ID); ?></span>
        </div>
        <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
    
    <?php endif; ?>
Viewing 6 posts - 1 through 6 (of 6 total)

The topic ‘Display custom field value from another post type’ is closed to new replies.