Metabox Populated with a Custom Post Type – How to Output CPT based on Select?

I have a dropdown metabox populated with a custom post type, now in my template, I can get the value of the metabox output but what I really need is all the information stored from that selected CPT to be displayed in certain areas of the post.

My metabox

Read More
$meta_boxes[] = array(
'id' => 'actordetails',
'title' => 'Select an Actor',
'pages' => array( 'films' ),
'context' => 'normal',
'priority' => 'high',

// List of meta fields
'fields' => array(

    array(
'name' => '',
'id' => $prefix . 'getactors',
'type' => 'select',
'clone' => false,
'options' => get_actors_options(),
    ),


)
 );

My function

function get_actors_options( $query_args ) {

$args = wp_parse_args( $query_args, array(
    'post_type' => 'actors',
) );

$posts = get_posts( $args );

$post_options = array();
if ( $posts ) {
    foreach ( $posts as $post ) {
        $post_options [ $post->post_title ] = $post->post_title;
    }
}

return $post_options;
}

This is what I get with:

    <?php echo get_post_meta($post->ID, 'nt_getactors', true); ?>

enter image description here

This is what I need:

enter image description here

So what code would I use to get the rest of the custom post type that I selected.

Related posts

1 comment

  1. If you set the value of the metabox to the authors cpt post id, you should be able to get the post with

    //get the id for the actors cpt
    $actors_id   = get_post_meta( $post->ID, 'nt_getactors', true );
    
    //get the post obejct for the author
    $actors_post = get_post( $actors_id, OBJECT ); //or ARRAY_A if you want an array and not an object
    
    //to output e.g. the title use
    $actors_post->post_title;
    

    See get_post for further options.

    Update

    Change

    $post_options [ $post->post_title ] = $post->post_title;
    

    to

    $post_options [ $post->ID ] = $post->post_title;
    

    Update 2

    Use your $actors_post post object just like a normal post object. Check the codex for a nice reference of the available member variables.

    e.g.

    $actors_post->post_content;
    

    Keep in mind that $actors_post data is “raw” and you might want to apply some filters to it, depending on how you are using it; e.g.

    apply_filters( 'the_content', $actors_post->post_content );
    

    Update 3

    In order to get meta values from the actor post either do this for a single value

    get_post_meta( $actor_post->ID, 'ecpt_bio', true );
    

    or (if you have multiple values you can get them all in one array) like this:

    $actor_meta = get_post_meta( $actor_post->ID );
    //and then access the array element
    echo $actor_meta['ecpt_bio'];
    

    Wrap up

    Change your callback function to reference the id of the post instead of the title (see Update#1)

    //get the id for the actors cpt
    $actors_id   = get_post_meta( $post->ID, 'nt_getactors', true );
    
    //get the post obejct for the author
    $actors_post = get_post( $actors_id, OBJECT );
    
    //to output e.g. the title use
    echo apply_filters( 'the_title', $actors_post->post_title );
    
    //output the content
    echo apply_filters( 'the_content', $actors_post->post_content );
    
    //get the meta values from the actor post
    $actor_meta = get_post_meta( $actor_post->ID );
    
    //and output it like this
    echo $actor_meta[ 'ecpt_bio' ];
    

Comments are closed.