I’m trying to retrieve all the attachment of a specific post, but it doesn’t work for the moment. Here is the code :
$args = array(
'post_type' => 'attachment',
'posts_per_page' => -1,
'post_parent' => $id
);
$attachments = get_posts($args);
Where Id is the id of my post. I’ve tried also with new WP_Query() but it didn’t worked neither. Both ways return an empty result.
Does anyone have an idea what I’m doing wrong here ?
Thanks
EDIT
Ok, I think I know what’s going wront.
When using this arguments for the get_posts function, it will return the images uploaded through the “Add a media” button in the specific post.
So basically, let’s say that on my first post, I’ve uploaded all the images I would need for all my future post. If I apply the request on the first post, I will retrieve all the images, even the one that I don’t use in this post.
If I apply this function to another post, because I didn’t uploaded any file in this post, the function will retrieve an empty array.
Does anyone have an idea how I can retrieve all the images used in a specific post ? So not only uploaded, but really integrated into the post or added to a custom field ?
Thanks
When using
get_posts
to fetch attachments, you need to setpost_status
toinherit
. Also, check out theget_children
function:Case: using ACF, I created an repeater field ‘resort_pics’ for my gallery, which has 2 fields ‘picture_title’ as text and ‘picture’ as an picture type.
Then happily uploaded 100 photos, some of them were same for several posts so I only clicked those from uploaded gallery (I will use term of “referenced images” for those).
Problem : then one happy day I noticed all “referenced images” are missing in our api.
Cause :
As noted at documentation comment from ‘Uriahs Victor’ (https://developer.wordpress.org/reference/functions/get_attached_media/)
Real cause :
Real source of all this problem is The thing that information about “reference images” are not stored in ‘wp_posts’ table but are actually stored in ‘wp_postmeta’, so by just querying ‘wp_posts’ table or using get_attached_media() which only looks there also you will not get all attachements for post.
Solution :
Lets take an example of post with ID – $postId which has defined
repeater – ‘resort_pics’, field in repeater with image ‘picture’. (created with ACF plugin)
First get all attachements for our post (resort) (including images/pdfs and so on) with classic way (can be also used ‘get_attached_media’):
where guid is ‘url’ to an attachement, lets index those in array where key will be post id of an attachement
Now we have all atachements but are missing all referenced images/files.
Lets continue by selecting all meta for our post (resort).
And index them by an meta key
Lets say our post ($postId) has an 9 entries in ‘resort_pics’ with an image uploaded to its ‘picture’ field, then $mapMetas[‘resort_pics’] will have an value of 8.
Now the way how repeater fields keys are stored in $mapMetas array, is actually an :
Knowing this we can get simply all image urls for “resort_pics”
You may already get to this point, simply from $mapMetas get image ID and using it get an image url from $mapImages.
Lets say ‘resort_pics_1_picture’ is ‘referenced’ one (not directly uploaded image), we have its id ‘5641’ but since its not connected to our $postID but to some other post id when it was actually uploaded. Its missing in our $mapImages array, so lets edit this code a bit.
We have added an getAttachementOrItsReferenceUrl() method, which will simply first check if we already have this image (all uploaded to this post) and if not will fetch image data by its post id from ‘wp_posts’ table..
Finnal thoughts :
If you know your fields structure you can build up its key pretty straightforward
For an example
‘rooms’ repeater which has inside ‘room_gallery’ repeater which has inside ‘image’ image field.
How heavy is it ? Not really, if you are like me, having only small amount of referenced images you wont even feel it. All we added to load is an one meta query for an post, if no images are referenced that’s all, then for every referenced image there is one more query, but its query on ID should be fast. There is a lot of way how to make load less, for example not do query for every single missing image, but do single query at the end with ‘where in (id1,id2..)’ to have single query to get all, or after fetching new image data, store it in some global array by its id, and check this array before fetching image for next posts (so if one image is stored in 200 posts, and you are fetching all it would make only 1 query), you got the point I leave it just at the ideas since this is bloody long post even without that 🙂
Hope this will help anybody in future.