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
$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); ?>
This is what I need:
So what code would I use to get the rest of the custom post type that I selected.
If you set the value of the metabox to the authors cpt post id, you should be able to get the post with
See get_post for further options.
Update
Change
to
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.
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.
Update 3
In order to get meta values from the actor post either do this for a single value
or (if you have multiple values you can get them all in one array) like this:
Wrap up
Change your callback function to reference the id of the post instead of the title (see Update#1)