Get PDF in media by ID

Question

On my search page, I’ve added all the file from the media with this code :

    function attachment_search( $query ) {

    $args = array(
        'public'   => true,
        '_builtin' => false,
    );

    $output = 'names';
    $operator = 'and';

    $cpts = get_post_types( $args, $output, $operator );
    $cpts[] = 'attachment';
    $cpts[] = 'post';
    if ( $query->is_search ) {
        $query->set( 'post_type', $cpts );
        $query->set( 'post_status', array( 'publish', 'inherit' ) );
    }

    return $query;
}

add_filter( 'pre_get_posts', 'attachment_search' );

In the media, i’ve added a sample.pdf file.

The sample is showing, but is shown as a post. I want to only add the dowload link from the PDF. But how to I do this ? wp_get_attachment_url(get_the_ID()) is not working, as the post ( = the PDF ) has no attachment file ?

PS : this is my content-search.php

<?php    
$post_type = ucfirst(get_post_type());

$download = "";
//if(get_post_type() == "attachment"){
    $dowload = wp_get_attachment_url(get_the_ID());
//}
?>

<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    <header class="entry-header">
        <h2 class="entry-title m-0">
            <a href="<?=esc_url( get_permalink() ); ?>"  rel="bookmark">
                <?=the_title();?> <?= "(".$post_type.")"; ?>
                " <?php echo $download;?> " <?php the_ID(); ?>
            </a>
        </h2>
    </header><!-- .entry-header -->
</article><!-- #post-<?php the_ID(); ?> -->

The wp_get_attachment_url(get_the_ID()) is returning “” for any document for this file only… Image or PDF.

0
, Morgan Tartreau 4 years 2020-03-27T12:51:23-05:00 0 Answers 97 views 0

Leave an answer

Browse
Browse