I have a custom meta box to store pdf files. For this input, I’m using type=”media” to acess wordpress media library.
Now I can’t get the file name. Here is my entire loop:
<ul>
<?php
$args_artigos = array('post_type' => 'artigo','posts_per_page' => -1);
$artigos_posts = new WP_Query($args_artigos);
if($artigos_posts->have_posts()) :
while($artigos_posts->have_posts()) :
$artigos_posts->the_post();
?>
<li>
<a href="<?php the_permalink(); ?>" class="artigos-titulo"><?php the_title() ?></a>
<span class="artigos-autor"><?php echo esc_html( get_post_meta( get_the_ID(), 'autor', true ) ); ?></span>
<?php $filename = basename ( get_attached_file( get_the_ID() ) ); echo $filename; ?>
</li>
<?php endwhile; else: ?>
Oops, there are no posts.
<?php endif; ?>
</ul>
But it’s return no data.
Your meta field contains the ID of the attachment, you need to fetch that ID, then get the filename using that attachment ID.