Gel all image from certain post type

I’ve found similar questions here, however I can’t modify the answers to get images only from a certain post type – as the post type in those examples are “attachment” and not the name of a custom post type.

Is there a way to declare a custom post type argument as well?

Read More

Similar question,
another similar question

Related posts

Leave a Reply

1 comment

  1. Try this code in your template.

    $query = new WP_Query( array( 'post_type' => 'custom-post', 'posts_per_page' => -1 ) );
    if( $query->have_posts() ){
        while($query->have_posts()){
            $query->the_post();
            $image_query = new WP_Query( array( 'post_type' => 'attachment', 'post_status' => 'inherit', 'post_mime_type' => 'image', 'posts_per_page' => -1, 'post_parent' => get_the_ID() ) );
            while( $image_query->have_posts() ) {
                $image_query->the_post();
                echo wp_get_attachment_image( get_the_ID() );
            }
        }
    }
    

    Replace custom-post with you custom post type.