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?
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).