Daniy Image Manager Plugin Saves Your WordPress Queries for Attachments

Blogging with lots of images on each post is surely a repetitive task where in some time will burn you out. The task for opening and inserting images to the post is a boring task. For you who have a few WordPress coding skills, you can write a piece of code to call for the current post attachments and output it to the browser. I have this piece of code, once referred by Isnaini from a website which is now currently available. With a function called the_image(), any number of images attached to a post will be automatically listed along with the single page view.

Post Attachments GalleryPost Thumbnail Screenshot

One thing I found later, that it costs a bit of WordPress queries to call each those attachments off the database. Getting attachments with get_posts() function is one query, extracting its size and file location is another call to wp_postmeta where all data about an attachment is stored. So if you have 20 images in a post, you’ll end up with additional 21 queries. More queries means more load time for your server to response a request from browser. Bad.

So, now I improved those snippet so that every request to attachment in database will be only fired up once, and then cached silently into WordPress main posts table, the wp_posts table. This table and the corresponding table row is ALWAYS called everytime a post is requested, which means we don’t need to make additional queries just to get post attachments.

Now, here’s the plugin… Download from the WordPress.org repository


Once you download, upload and install it on your WordPress, go to the text editor and put this single line of function in single.php right after <?php the_content(); ?> tag, to get ALL the post attachments

<?php imwp_view_gallery(); ?>

And in your archive.php, or category.php, put this function in a WordPress Loop to call the post thumbnail

<?php imwp_view_thumbnail(); ?>