Site icon Code Solution

Get all post attachments except featured image

The question:

$args = array(
    'post_type' => 'attachment',
    'numberposts' => null,
    'post_status' => null,
    'post_parent' => $post->ID
);
$attachments = get_posts($args);
if ($attachments) {
    foreach ($attachments as $attachment) {
        echo apply_filters('the_title', $attachment->post_title);
        the_attachment_link($attachment->ID, false);
    }
}

In the code above, theres exist a way to get:

  1. All attachments links except featured image.

  2. All PDF attachments links only.

I was reading:

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

For the first one, you can add 'exclude' => get_post_thumbnail_id() as a parameter (as shown here).

For the second one, you can add 'post_mime_type' => 'application/pdf', but I’m not sure that would always work, afaik, pdfs have more than one mime type.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Exit mobile version