Home › Forums › Add-ons › Gallery Field › “Reverse Lookup” via Attachment Custom Field › Reply To: “Reverse Lookup” via Attachment Custom Field
Here’s what I think is the complete rundown:
I’m using this URL:
/testing/?image_id=8324
There’s nothing on that page except for:
[test_shortcode]
And just to confirm I’m using the correct field name, here’s the source code of the field in the backend when editing an image:
<tr class="acf-field acf-field-number acf-field-64e143e0efab0" data-name="image_meta_image_id" data-type="number" data-key="field_64e143e0efab0">
<td class="acf-label">
<label for="acf-field_64e143e0efab0">Image ID</label></td>
<td class="acf-input">
<div class="acf-input-wrap"><input type="number" id="acf-field_64e143e0efab0" name="acf[field_64e143e0efab0]" value="8324" step="any"></div></td>
</tr>
And here’s the complete shortcode:
function get_test_shortcode() {
$image_meta_image_id = $_GET["image_id"];
// echo $image_meta_image_id;
// *********************** FROM ACF FORUM 09-01-2023 ***********************
// 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( $args );
$attachments = $attachment_query->posts();
echo "<pre>";
var_dump( $attachments );
echo "</pre>";
}
add_shortcode( 'test_shortcode', 'get_test_shortcode' );
The only thing showing on the page is the page title TESTING and bool(false).
Do you see anything amiss?
Thanks again!!
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.