Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a custom post type for a real estate site, and the theme displays 9 properties in a grid on the home page. I don't think its written terribly efficiently - I'm getting about 160 queries on the front page, and only about 20 of those queries are from plugins, so the theme needs some improvement.

In the first place, it includes a file in the post loop - I'm not a PHP expert, but is including the same file repeatedly in the post loop really the best thing to do?

The file that is included simply calls get_post_meta() for about a dozen or so variables for the custom post property listing (eg, number of bedrooms, etc). My first proposal is to move that section of code into the main file so its not "included" over and over.

But more importantly, is there a better way to get the post meta than to call get_post_meta() a dozen times for a dozen variables - perhaps with a single query that gets all the meta at once, which I can assign to an array?

share|improve this question
    
It really depends what is in that include, including over and over isn't necessarily bad, it depends what code is being repeated over(though i'll admit there's usually a better way than repeating an include). –  t31os Aug 18 '11 at 20:44

1 Answer 1

up vote 4 down vote accepted

You can use get_post_custom() which will return an array of all post meta in one single call to the database, that should save a few calls.

And as for including the file over and over you could use get_template_part() to include a file once which will hold the entire loop (a good example can be found in the default twenty ten or eleven theme).

share|improve this answer
    
Thanks - I rewrote my code to use both your suggestions. It didn't seem to reduce the number of queries though? –  Amanda Aug 19 '11 at 13:23
1  
Then i can only guess they come from somewhere else, try installing debug bar and see what these queries are. –  Bainternet Aug 19 '11 at 16:36

Your Answer

 
discard

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.