Using a Custom Post Type Attachment as link

I have a pretty simple custom post type (“weekly_mktg_report”) that has two fields – date(“date”) and attachment(“report”). I’ve created an archive page that uses

<a href="https://wordpress.stackexchange.com/questions/284647/<?php the_field("report'); ?>" target="_blank">

This creates a link to the attachment and not the actual custom post, which is perfect for what I’m trying to accomplish.

I’m trying to do the same thing on another page that link to the attachment of the most recent of these custom post types.

This is what I tried, but isn’t working

<?php
$args = array( 
    'numberposts' => '5',
    'orderby' => 'post_date',
    'order' => 'DESC',
    'include' => '',
    'exclude' => '',
    'meta_key' => '',
    'meta_value' =>'',
    'post_type' => 'weekly_mktg_report',
    'post_status' => 'publish' 
);

$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
    echo '<li><a href="' . get_permalink($recent["ID=>the_field('report') "]) . '">' .   $recent["post_title"].'</a> </li> ';
}
wp_reset_query();
?>

I know there’s something wrong w/ the way I’m trying to append my get.parmalink to go to the attachment (“report”) instead of the actual custom post page. Any help would be greatly appreciated.

1 Answer
1

you don’t want the link to the actual post then use the field link again:

<?php
$args = array( 
    'numberposts' => '5',
    'orderby' => 'post_date',
    'order' => 'DESC',
    'include' => '',
    'exclude' => '',
    'meta_key' => '',
    'meta_value' =>'',
    'post_type' => 'weekly_mktg_report',
    'post_status' => 'publish' 
);

$recent_posts = wp_get_recent_posts( $args );
echo '<ul>';  //i was not sure if you had the list start anywhere else
foreach( $recent_posts as $recent ){
    echo '<li><a href="'.get_field('report').'" target="_blank"> </li> ';
}
echo '</ul>';  //list end
wp_reset_query();  //have you considered wp_reset_postdata() and using wp_query
?>

I put some comments in the code too as there may be some errors. Not sure if this is the full code.

Leave a Comment