Skip Navigation

[Resolved] Question about listing post attachments

This thread is resolved. Here is a description of the problem and solution.

Problem:

I want to display a list of PDF attachments that have been uploaded and inserted into the body of a post. However, in the Views interface, "attachment" is not available as a post type, making it unclear whether this can be done visually or if custom PHP is required.

Solution:

Toolset does not currently support attachments in the Views GUI. Instead, use custom PHP with get_posts() to retrieve attachments with post_type='attachment' and post_mime_type='application/pdf', filtering by post_parent to match the current post. Then, use a shortcode to display the list of PDFs below the post content.

Relevant Documentation:

https://toolset.com/forums/topic/question-about-listing-post-attachments/#post-2795046

https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

This topic contains 1 reply, has 1 voice.

Last updated by Saul Baizman 4 months, 1 week ago.

Assisted by: Christopher Amirian.

Author
Posts
#2794922

Hi there,

We had a question about listing post attachments. Our objective is to display a list of PDFs that have been uploaded and inserted into the body of a post. (The list of PDFs will appear below the body in a separate section.)

In the Views interface, we don't see "attachment" as a post type. From our research, we see that we can write custom PHP and use get_posts() for a post_type="attachment" and where the post_parent is the current post ID. Is this possible to do from Toolset's visual interface via Views, or do we need to rely on PHP?

The code we're referencing is in this StackOverflow post: https://stackoverflow.com/questions/42917812/how-to-get-all-attachments-for-a-wordpress-post.

Thank you!

Saul

#2795046

Christopher Amirian
Supporter

Languages: English (English )

Hi Saul,

Welcome to Toolset support. There is no GUI feature to add attachments to a post in Toolset.

You can use Toolset to add a custom PHP code to achieve this:

https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code

The PHP should be something like this:

function get_post_pdfs() {
    global $post;
    $args = array(
        'post_type'   => 'attachment',
        'post_mime_type' => 'application/pdf',
        'post_parent' => $post->ID,
        'posts_per_page' => -1
    );
    $attachments = get_posts($args);

    if ($attachments) {
        $output = '<ul>';
        foreach ($attachments as $attachment) {
            $url = wp_get_attachment_url($attachment->ID);
            $title = get_the_title($attachment->ID);
            $output .= "<li><a href='$url' target='_blank'>$title</a></li>";
        }
        $output .= '</ul>';
        return $output;
    }
    return '<p>No PDFs found.</p>';
}
add_shortcode('post_pdfs', 'get_post_pdfs');

Then, add this shortcode to the post content or template:

[post_pdfs]

The code is not tested, but can give a good starting point for you.

Thanks.

#2795050

Christopher,

This is fantastic. Thanks so much.

I might request, as a feature, that attachments be incorporated into Views. They're a native WordPress post type, and it would be great to be able to control the display of them via Toolset.

Thanks again!

Saul