custom post types - Get id from metabox dropdown

admin2025-06-03  3

Im using Reusable Metaboxes (old but still works) and I created a metabox that allows the user to pick a post from a list of custom post types. Right now that works pretty well, I can see all the posts in a list and I pick one and save.

The data I see when I call the_meta() shows me that the field is indeed saving the id of the post I called:

episode_guestid: a:1:{i:0;s:2:"18";}

But I don't know how to get the id (in this case 18 - and this needs to be dynamic depending on the chosen post) and use it to display things from that post: title, featured image, metaboxes content.

Tried the "easiest one" with no success (so I will need help with figuring out how to display the metabox data from that post as well):

$guestid = $post_meta_data['episode_guestid'][0]; ?>
<h1> Featuring: <?php echo unserialize($guestid);  ?> </h1>

Hope someone can help me out with this one. Thanks!

Im using Reusable Metaboxes (old but still works) and I created a metabox that allows the user to pick a post from a list of custom post types. Right now that works pretty well, I can see all the posts in a list and I pick one and save.

The data I see when I call the_meta() shows me that the field is indeed saving the id of the post I called:

episode_guestid: a:1:{i:0;s:2:"18";}

But I don't know how to get the id (in this case 18 - and this needs to be dynamic depending on the chosen post) and use it to display things from that post: title, featured image, metaboxes content.

Tried the "easiest one" with no success (so I will need help with figuring out how to display the metabox data from that post as well):

$guestid = $post_meta_data['episode_guestid'][0]; ?>
<h1> Featuring: <?php echo unserialize($guestid);  ?> </h1>

Hope someone can help me out with this one. Thanks!

Share Improve this question asked Feb 7, 2019 at 18:09 artist learning to codeartist learning to code 3311 gold badge5 silver badges18 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

the_meta is a very bare-bones method. The return value you're seeing is raw, unserialized data. WordPress uses PHP serialization to store complex values like Arrays and Objects as a string value. Have a look at get_post_meta:

$guest_id   = get_post_meta( get_the_ID(), 'episode_guestid', true );
$guest_post = get_post( $guest_id[0] );
echo '<pre>';
print_r( $guest_post );

You should see the post you're looking for. Let me know if that doesn't work.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748934185a314948.html

最新回复(0)