how to get serialized post meta

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

Topic post-meta phpmyadmin wpdb php custom-field Wordpress

Category Web


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.)

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.