7

This is trivial, but I can't really find an answer, I've seen some hints like search in database etc., but this is madness.

Basically, I have a metabox with upload button that allows you to upload or pick existing image from Media Library, that's how I get the URL of this image:

$my_image = get_post_meta($post->ID, '_my_image_metabox', true);

Of course I do want exact size of this image to be displayed so I need to use:

wp_get_attachment_image_src($attachment_id, 'desired-size');

The question is how do I get attachment_id of _my_image_metabox? I've been searching literally everywhere and there are no sane answers, how is wp_get_attachment_image_src() supposed to work if there's not a single line on getting $attachment_id in Codex whatsoever? :)

[updated]

var_dump($my_image) returns:

array(1) { [0]=> string(86) "http://localhost/theme/wp-content/uploads/2014/10/my_image.jpg" }

Saving the data is pretty complicated as I use my own metabox generating class, something like this: https://github.com/nyordanov/SmartMetaBox/blob/master/SmartMetaBox.php

How do I inject the proper attachment_id to my_image then? I'd add I have multiple (10) fields like this one and would like every single one to have separate, unique attachment_id.

6
  • 2
    The meta box probably does not have an attachment ID associated with it unless you add it, $my_image is just a string. Set get_post_meta to false and debug what it actually returns.
    – Wyck
    Oct 31, 2014 at 0:54
  • 1
    Please update the question and show us how you save the data. Retrieval should be fairly easy then.
    – kaiser
    Oct 31, 2014 at 2:06
  • Thanks, question updated :) I don't need to have an attachment_id here though, I just need to get proper size for the image added to metabox (it is in Media Library so has proper thumbnails), but I think that's the only way (excepting regexp). Oct 31, 2014 at 14:28
  • There is Turn a URL into an Attachment / Post ID, which could be helpful. Aside from that, you'd have to explicitly save the the id, like @Wyck said. Oct 31, 2014 at 14:34
  • I've been working it out for quite a while and I'm wondering - how can I get the proper ID here? Nov 23, 2014 at 19:32

6 Answers 6

9

Ok, all those hipercomplex functions can be reduced to one simple command: attachment_url_to_postid

You only need to parse the image url to retrieve the attachment id:

<?php

   $attachment_id = attachment_url_to_postid( $image_url );
   echo $attachment_id;

?>

That's all you need.

1
  • This is BY FAR the best answer. The reason being that no other function like url_to_postid or get_page_by_path will return an ID if the the media is attached to a post.
    – SlickRemix
    Jan 8 at 19:15
2

I am too late to answer this question but it will worked.

$attachment_id = get_post_thumbnail_id( $post->ID );
$url = wp_get_attachment_image_src($attachment_id, 'desired-size');
1

I had the same problem while I was using CMB plugin, I solved adding a custom function to functions.php to get the image ID using the image url:

functions.php

function cmb_get_image_id($image_src) {
    global $wpdb;
    $image = $wpdb->get_col($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE guid='%s';", $image_src )); 
    return $image[0]; //return the image ID
}

In your theme:

$my_image = get_post_meta($post->ID, '_my_image_metabox', false);
$attachment_id = cmb_get_image_id($my_image);
wp_get_attachment_image_src($attachment_id, 'desired-size');
1

What I do in such cases in that save the id in a hidden input along with the URL.

function meta_image_upload() {
    global $post;
    $my_image = get_post_meta($post->ID, '_my_image_metabox', true);
    $my_image_id = get_post_meta($post->ID, '_my_image_id_metabox', true);

    $out = '<input id="my_image" type="text" name="my_uploaded_image" value="'. $my_image .'">';
    $out .= '<input id="my_image_id" type="hidden" name="my_uploaded_image_id" value="'. $my_image_id .'">';
    $out .= '<button id="my_image_upload">Upload Image</button>';

    echo $out;
}  

On the upload handler script:

attachment = custom_uploader.state().get('selection').first().toJSON();
    $('#my_image').val(attachment.url);
    $('#my_image_id').val(attachment.id); // Just add this line

Then at other callbacks :

global $post;
$my_image_id = get_post_meta($post->ID, '_my_image_id_metabox', true);
wp_get_attachment_image_src($my_image_id, 'desired-size');

That's just my solution and someone might come up with a better one. However, I found it faster than querying database to fetch the associated attachment id.

0

All the post meta you're saving is being attached to a post of type attachment, so You can consider $post->ID is the attachment ID.

-1

Try with this:

$attachment_id = $wpdb->get_var( 
    $wpdb->prepare( 
        "SELECT wposts.ID 
            FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta 
            WHERE wposts.ID = wpostmeta.post_id 
            AND wpostmeta.meta_key = '_wp_attached_file' 
            AND wpostmeta.meta_value = '%s' 
            AND wposts.post_type = 'attachment'", 
        $url
    ) 
);
1
  • 2
    Please explain what your code does and how it works. Also format your code, it is unreadable Sep 9, 2015 at 6:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.