I'm trying to show all the posts on a page with an audio player in it and for that I'm looping through posts in the following manner:

<?php

$posts = get_posts();
$ID;
foreach( $posts as $item)
{
 global $post;
 $post_id = $item->ID;
 $file_id = get_post_meta($post_id, 'sound_s', true);
 $file_url = wp_get_attachment_url( $file_id );
 $attr = array(
'src'      => $file_url,
'loop'     => '',
'autoplay' => '',
'preload'  => 'none'
    );
    echo wp_audio_shortcode( $attr );

    $ID = $file_id;
    var_dump($file_id);
}

?>

But trying to echo or var_dump $file_id is returning in null due to which my audio player can not be shown. And yeah there's no mistake in meta key and $post_id is also returning currect value. So please tell me why is this happening?

  • 2
    The code you've given seems fine. Your site must not be storing this value the way you think it is. – Jacob Peattie Sep 13 at 13:12

Please try below code and check if 'sound_s' key exists in the array or not

$file_id = get_post_meta($post_id);
echo "<pre>";
print_r( $file_id );
exit;

Have you try to change the third attributes of get_post_meta() to false

$file_id = get_post_meta($post_id, 'sound_s', false);

and then access your variable by

var_dump($file_id[0]);

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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