Forum Replies Created

Viewing 1 replies (of 1 total)
  • I used Joy’s code from above, but it wasn’t working at first. I didn’t use the Import Legacy Media that she mentioned though – that might have been the problem. I used WP’s (ver 3.4.1) built in media uploader.

    After a little inspection of the database, I found out what was going on – the WP uploader was adding the year & month directory names to the name of the image in the wp_postmeta table. (instead of the expected “image.jpg” it was “2012/8/image.jpg”)

    Once I realized this, a quick change of the SQL query made it work perfectly.
    Change this:

    $imageID = $wpdb->get_var("SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '$k' AND meta_value = '$v'");

    to this:

    $imageID = $wpdb->get_var("SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '$k' AND meta_value LIKE '%$v%'");

    Don’t forget the double %’s around the $v. Now the query will search for any value that CONTAINS your filename instead of EQUALS your filename.

    Hope this helps!

Viewing 1 replies (of 1 total)