0

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?

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

2 Answers 2

0

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;
-1

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]);
1
  • Sorry but It didn't work. Commented Sep 14, 2018 at 3:29

Your Answer

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

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