WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I currently have configured Wordpress so that a post can be copied to other sites within a Wordpress network. However, a post may be broadcast more than once, and I want to check if the image is already published into that site's wp_posts table.

As an example, let's say an image has the image path:


All images are stored by the network within this setup of folders (rather than the default /blogs.dir/files/).

In other words, can Wordpress query the database to check if this is already in the table, or does it need to be written as a PHP database query?

share|improve this question
Please make clear what you already know (DB ID, Blog ID, etc.) and what you want to know. – kaiser Jan 2 '13 at 13:43
@kaiser Only the URL of the file is known. – Ollie Jan 2 '13 at 20:14
up vote 2 down vote accepted

Yes, a database query is possible with WordPress:

function is_image_in_network( $image_name )
    global $wpdb;
    $image = "uploads/2012/01/{$image_name}";
    $value = '%'.like_escape( $image ).'%';
    $blog_id = get_current_blog_id();
    $wpdb->set_blog_id( $blog_id );
    $image = $wpdb->get_var( 
            "SELECT ID FROM {$wpdb->posts} 
            WHERE  post_status = 'publish' 
            AND post_content LIKE '%s'"
            , $value 
    return is_null( $image ) ? false : true;

You can use it as function in your templates. Just add the image name, you're searching for as argument. The new "Template Tag" works like every default WP "Conditional Tag".

Is that image.php in the Question a typo?

I'm leaving the search string %uploads/2012/01/an_image% without an extension so it can catch all the images (original and thumbnails).

Documentation: Class_Reference/wpdb.

share|improve this answer
+1, but you're missing the like_prepare(). – kaiser Jan 2 '13 at 17:42
@kaiser, like what¿! I cannot find that in google or core :s – brasofilo Jan 2 '13 at 18:41
Sry, replied to Rarst & toscho in chat something with prepare(). :P Actually it's like_escape() and you'll likely need to add it like this: '%'.like_escape($foo).'%'. – kaiser Jan 2 '13 at 18:44
@kaiser, one more to the function bag, looks like escaped now, thx. I cannot help but see that this function is staring at me e_e ;) – brasofilo Jan 2 '13 at 19:52
@kaiser, at all, you just made those lines of code happier, now they belong, liko.Oscape() – brasofilo Jan 2 '13 at 23:55

The easiest way should be to use wp_attachment_is_image().

if ( ! wp_attachment_is_image( $post_ID ) )
    // process needed tasks

Note that this only takes those files into account that have a header of image/* or an extension of 'jpg', 'jpeg', 'jpe', 'gif', 'png'.

This is not tested.

share|improve this answer
Unfortunately I won't know the ID in the database, only the image URL. Any ideas here? – Ollie Jan 2 '13 at 13:16
"(...) I won't know the ID (...)" ... do you don't want to know or you don't know ... ? – kaiser Jan 2 '13 at 13:42
I don't know - I'm checking whether an image I'm taking from another blog in the network is in the blog I am checking. – Ollie Jan 2 '13 at 20:22
1st: How are you "taking" this image? 2nd: @brasofilo has a working query for you. – kaiser Jan 2 '13 at 22:40

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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