I don’t think there is a way to query for this directly by normal methods.
As rzysiek Dró?d? said you can do multiple queries, something in like this:
<?php
// change this to your custom post type
$post_type = 'book';
$args= array(
'post_type' => 'attachment',
'post_status' => 'inherit',
'post_mime_type' => 'image',
'fields' => 'id=>parent',
'posts_per_page' => -1
);
// get all image parent post ids
$image_parents = get_posts( $args );
// remove any parent posts with post id 0
$image_parents = array_filter( $image_parents ); // query
if ( $image_parents ) {
// get all post ids of post type
$parent_posts = get_posts( 'fields=ids&post_type=' . $post_type ); // query
if ( $parent_posts ) {
// get all parent posts by post type that have images
$parent_posts_with_images = array_intersect( $parent_posts, $image_parents );
if ( $parent_posts_with_images ) {
// loop through all post type posts that have images
foreach ( $parent_posts_with_images as $post_id ) {
// your code to show the images
$args = array(
'post_parent' => $post_id,
'post_status' => 'inherit' ,
'post_type' => 'attachment',
'post_mime_type' => 'image',
'posts_per_page' => -1
);
$images = get_children( $args ); // query for all posts with post types
if ( $images ) {
foreach ( (array) $images as $image ) {
print wp_get_attachment_url( $image->ID );
}
}
}
}
}
}
?>
Or by a custom query:
<?php
// change this to your custom post type
$post_type = 'book';
global $wpdb;
$where = get_posts_by_author_sql( $post_type );
$query = "SELECT * FROM $wpdb->posts p where p.post_type = 'attachment' AND (p.post_mime_type LIKE 'image/%') AND (p.post_status = 'inherit') AND p.post_parent IN (SELECT $wpdb->posts.ID FROM $wpdb->posts {$where} ) ORDER BY p.post_date DESC";
$results = $wpdb->get_results( $query );
if ( $results ) {
foreach ( (array) $results as $image ) {
print wp_get_attachment_url( $image->ID );
}
}
?>