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:
/wp-content/uploads/2012/01/an_image.jpg
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?
Yes, a database query is possible with WordPress:
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.
The easiest way should be to use
wp_attachment_is_image()
.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.