Support » Plugins » Hacks » Get post from image URL

  • Hi everyone ! No luck with research today : All I get is exactly what I need but the other way around. πŸ˜€

    I’m looking for a way to retrieve posts containing (and/or featuring) a specific picture.

    If it’s unclear, here is an example :
    Media Library contains the 3 following uploaded images :

    Post ID 3882 has a.jpg as thumbnail, and c.jpg is displayed in post content.
    Post ID 4290 has no thumbnail image, but post content contains a.jpg.

    What I’m trying to do is a function like this :

    function get_post_from_image($image_url) {
    //returns $post->ID or $post object, it obviously doesn't matter
    }
    
    print_r( get_post_from_image( 'http://blog/wp-content/uploads/2012/08/a.jpg' ) );
    // array(3882, 4290)
    
    print_r( get_post_from_image( 'http://blog/wp-content/uploads/2012/08/c.jpg' ) );
    // array(3882)

    I could search the whole wp_posts table in plain-text to find searched image URL occurences, but I like to dream of a better way to do this.

    Any idea ?

    Thanks a lot

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    Interesting question. If the urls in content are the only way to identify a post by image, of course you have to search all content, which could be a LOT of text.

    You could create post meta that contains image references, then on page requests, only need to search a specific post meta instead of all content. A sort of pre-indexing for search. Filter on new or edited posts, check for specific image content and add/remove post meta as needed. For existing posts, run a one time query to find specific image urls and create associated post meta.

    That’s my idea, be interesting to see other ideas.

    Thread Starter vhf

    (@kortchnoi)

    Thanks for your answer.

    Yeah, a lot of text… It would surely be too heavy to be worth it, i.e. add a not-so-important-but-still-quite-funny functionality to a plugin I’m developing.

    Being a plugin, I don’t want to add custom fields to all existing posts and pages and fill the user’s database with this. But I have to admit I hadn’t thought of pre-indexing, which still is a nice idea. I could try to implement this index, but probably with a custom table instead of filling wp_meta with custom fields’ values.

    Anyway… I thought I had a lead earlier sneaking around in WP’s DB and building this query : SELECT p1.guid as image_url, p1.post_name as post_name1, p2.post_title as post_title, p2.ID as post_id, p1.post_mime_type as image_mime FROM wp_posts as p1 INNER JOIN wp_posts as p2 ON p1.post_parent = p2.ID WHERE p1.post_mime_type != '' AND p2.post_status ='publish' ORDER BY p1.ID DESC
    I didn’t know that uploaded pictures where also given an entry in wp_posts… quite surprising. Looks like it’s done to link the files to the posts they were uploaded on. Although it seems this request doesn’t show files attached to a post but not inserted into its content nor used as thumbnail…

    I’ll keep digging.

    [ Please do not bump, that’s not permitted here. ]

    Thread Starter vhf

    (@kortchnoi)

    Could a mod move this to WP-Advanced please ?

    Thanks

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Get post from image URL’ is closed to new replies.