5

I can get a list of the most recent image attachments like so:

$attachments = get_posts( array(
    'post_type' => 'attachment',
    'posts_per_page' => $number,
    'post_mime_type' => 'image'
) );

This returns all images that have been recently uploaded, whether they are attached to a post or page, or unattached through the Media menu.

What I'd like is to limit that list to only images that are attached to a custom post type I've created. (The custom post type is not that important; the same question could be: 'only images attached to Posts' or 'only images attached to Pages.')

I realize I could test each image after running the above query and eliminate any whose parent is not the right post type, but I want to return a specific number of images ($number), and this method could eliminate some or all of the returned images!

2
  • 1
    Perhaps the answer I provided on this post, a question asking how to get the number of attachment for a CPT, would work with some tweaks: wordpress.stackexchange.com/questions/37741/…
    – 5t3ph
    Jan 11, 2012 at 18:43
  • Thanks Steph. That looks like it would work. (And it's pretty much the approach I described in my last paragraph above.) It just seems inefficient to retrieve all attachments and then sift through them one by one. I'd rather be able to select the right ones from the start. But it looks like that's not possible without a custom SQL statement. Maybe I'm over-thinking it. Anyway, I now have two approaches to try and compare. Thanks!
    – Doug
    Jan 11, 2012 at 20:52

3 Answers 3

1

Did you try adding a filter to get_posts. This isn't tested, just a thought after a bit of searching :

 function my_filter( $query )
 {
      if( is_home() ) //Example, if u want to display recent images on home
      {
           $query->set( 'post_type', array( 'attachment', 'my_cpt' ) );
      }
 }
 add_filter( 'pre_get_posts', 'my_filter' );

EDIT : After rereading, I don't think it accomplishes what you're trying to do, I think it will display both attachments, and posts of your CPT.

Guess i'ill leave it here in case it gives you any ideas.

EDIT 2: The only other way besides filtering in PHP that I can think of would be a custom sql query, and then setting up the post data. EX :

$sql = "SELECT * FROM wp_posts
        WHERE post_type = 'attachment'
        AND post_parent IN (SELECT ID FROM wp_posts WHERE post_type = 'your-cpt')
        //ORDER BY";

 $posts = $wpdb->get_results( $sql, OBJECT );

 //loop - setup_postdata

More info: Wordpress Codex

1
  • Interesting idea. I never like writing custom SQL, but yours looks pretty straightforward. I'll give it a shot.
    – Doug
    Jan 11, 2012 at 18:24
1

First you get the recent modified posts for your CPT only. Then get their attachments. Off course you can have some logic to limit array of attachments.

$mycpt = get_posts(array(
                 'post_type'=>'mycpt',
                  'posts_per_page'=>-1,
                  'orderby'=>'modified'
                 ));

$attachments = array();
foreach($mycpt as $mycpt)
{
  $post_attachements = get_posts( array(
    'post_parent'       => $mycpt->ID,
    'post_type'         => 'attachment',
    'post_mime_type'    => 'image',   
    'posts_per_page'    => -1
) );
   foreach($post_attachements as $post_attachements){
     $attachments[] = $post_attachements;
    }

}
1
  • This might solve the issue but it will add a significant overhead and will be slow. Running custom SQL query through $wpdb with some JOINS will be much faster and efficient. Jul 18, 2015 at 13:32
-1

put this code within the loop in single custom post type

$attachments = get_posts( array(
    'post_parent'       => get_the_ID(),
    'post_type'         => 'attachment',
    'post_mime_type'    => 'image',
    'orderby'           => 'menu_order',
    'order'             => 'DESC',
    'posts_per_page'    => -1
) );

if ( $attachments ) {
    foreach ( $attachments as $attachment ) {
        $attID     = $attachment->ID;
        echo '<a href="'.wp_get_attachment_url( $attID ).'" alt="'.apply_filters( 'the_title', $attachment->post_title ).'" class="att-item">'.wp_get_attachment_image( $attID, 'thumbnail' ).'</a>';
    }
}
3
  • May I ask you to read the question? It is about uploads for a specific post type, not a specific single post.
    – fuxia
    May 16, 2013 at 6:42
  • yes, single custom post type codex.wordpress.org/Post_Type_Templates
    – timDesain
    May 16, 2013 at 6:45
  • Your code does not return all attachments for a single post type, eg. all new uploads for books, just for one post.
    – fuxia
    May 16, 2013 at 6:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.