[wp-hackers] Reusing queries

Nathaniel Taintor goldenapplesdesign at gmail.com
Fri Nov 12 19:27:11 UTC 2010


Try assigning variables in each of your conditionals. Its a real basic
optimization, but it can save you time and code. I don't know the specific
functions you're using, but something like this:

if ( ( $image = wp_get_attachment_image_src( $attachment_ID ) ) &&
     ( $image[1] >= $min_width ) && ( $image[2] >= $min_height ) )
     echo '<img src="'.$image[0].'" >';

There are a lot of functions which return data rather than simply
true/false, so you can simplify your code and the number of db queries a bit
by using them.

That said, I don't know how much you should be worrying about a couple extra
queries per page. I think all of the API functions are designed to be
optimized as far as query structure and indexes. 5 or 10 extra queries per
page should be nothing to stress out over, as long as you're hapy with the
code it took to get them.


Nathaniel Taintor, Designer/Developer
*Golden Apples Design*
http://goldenapplesdesign.com

@GoldenApples | 503.877.2880
goldenapplesdesign at gmail.com



On Fri, Nov 12, 2010 at 11:07 AM, William Davis <will.davis at gmail.com>wrote:

> The site I'm working on doesn't use embedded images with posts but instead
> uses a few functions to see if there are images attached to the post, if
> those images meet a number of parameters, and finally to display those
> images.
>
> So the call for an image usually looks something like this:
> if(has_image() && image_meets_params()) {
>        image();
> }
>
> The problem is, each of those functions queries the database, so the number
> of queries on a page can potentially explode.
>
> has_image() checks to see if there are any images attached to the post at
> all
> image_meets_params() grabs the first image and checks the meta to see if it
> is a horizontal or vertical image and checks the width
> and then finally image() displays the image or images.
>
> Is there a way to reuse some of these queries from one function to another,
> so for example if has_image returns true it then passes all the information
> onto image_meets_params? Or are these queries light enough that I need not
> even be worrying about it?
>
> Will
>
> William P. Davis
> will.davis at gmail.com
> 207.660.5342
> http://twitter.com/williampd
> http://www.wpdavis.com
>
>
>
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>


More information about the wp-hackers mailing list