Home › Forums › Add-ons › Gallery Field › “Reverse Lookup” via Attachment Custom Field › Reply To: “Reverse Lookup” via Attachment Custom Field
// first you do a query for attachments looking for the exact image_meta_image_id
$args = array(
'fields' => 'ids', // return only IDs to use in the next query
'posts_per_page' => 1, // there should only be one image
// with this "image_meta_image_id"
'post_type' => 'attachment',
'meta_query' => array(
array(
'key' => 'image_meta_image_id',
'value' => $image_meta_image_id
)
)
);
$attachment_query = new WP_Query();
$attachments = $attachment_query->posts();
// now you need to do a query to get all of the posts what use that attachment
// if it was found
if (!empty($attachments)) {
$args = array(
'post_type' = 'your post type',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'gallery field name',
'value' => '"'.$attachments[0].'"',
'compare' => 'LIKE'
)
)
);
} // end if attachments found
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.