I rewrote the function based on a few other posts I have seen online and now have the following code which works without any errors.
I am still open to any further suggestions on the best way to streamline this code even further it it can be.
Thanks again
function downloads_shortcode($atts){
extract(shortcode_atts(array(
'download_type' => ''
), $atts));
// get the terms
$terms = get_terms( 'download_type' );
// no terms? bail.
if( ! $terms ) return '';
$out="";
// loop through the terms
foreach( $terms as $term )
{
// get videos in each term
$downloads = get_posts(array(
'post_type' => 'assoc-downloads',
'download_type' => $download_type,
'tax_query' => array(
array(
'taxonomy' => 'download_type',
'field' => 'id',
'terms' => $term->term_id,
'operator' => 'IN'
)
)
));
// no downloads? continue!
if( ! $downloads ) continue;
$out .= '<h2>' . esc_html( $term->name ) . '</h2>';
$out .= '<ul>';
// loop through the download posts
foreach( $downloads as $d )
{
$attachment_id = get_field('document', $d);
$url = wp_get_attachment_url( $attachment_id );
$title = get_the_title($d);
// part where to get the filesize
$filesize = filesize( get_attached_file( $attachment_id, $d ) );
$filesize = size_format($filesize, 2);
$out .= '<div class="ecd-single"><p><i style="font-size:120%; margin: 0px 6px 0px 3px;opacity:0.5" class="fa fa-file"></i><a href="'.$url.'" title="'.$title.'" class="ecd-download-file">' .$title.' </a> ('.$filesize.') <i style="font-size:120%; margin: 2px 0 0 5px;opacity:0.5" class="fa fa-download"></i> <a href="'.$url.'">Download</a></p></div>';
}
$out .= '</ul>';
}
return $out;}