0

I have posts that have video content and these videos stored in database like this (serialized) a:1:{s:9:"video_url";s:0:"";} This serialized data have post video urls but can not get this data. I used <?php echo get_post_meta(get_the_ID(), '_custom_field', TRUE); ?> But this get_post_meta display output "array" instead of video urls

1 Answer 1

1

get_post_meta() with the 3rd parameter set to true will return a single item with the key specified in the 2nd parameter (_custom_field in your code). If this item is an array, it'll return as the array. In your case, it appears that the data is:

array(
    'video_url' => '',
)

...which is why, when you try to echo it, it's printing Array to the screen.

I'd suggest something more like this:

<?php
$meta = get_post_meta(get_the_ID(), '_custom_field', TRUE);
echo $meta['video_url'];
?>

...or, if you're expecting multiple video_urls, you could do this:

<?php
$meta = get_post_meta(get_the_ID(), '_custom_field');
foreach ( $meta as $item ) {
    echo $item['video_url'];
}
?>

(Note that it also appears that, in the metadata you've posted, there's an empty value for the actual video_url, but I can't say what might have caused that.)

2
  • thank you very much for your attention. It is singe url. your code return empty value. coud it be that serialzed data get file url by file id from media library? May 1, 2022 at 21:23
  • The serialized data in your question shows an empty value. I can't guess what might be in your media gallery.
    – Pat J
    May 1, 2022 at 22:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.