[wp-hackers] Avoid query_post on frontpage on wp initialization
Mike Schinkel
mikeschinkel at newclarity.net
Mon Dec 27 21:33:24 UTC 2010
On Dec 27, 2010, at 4:07 PM, Martin Widmann wrote:
> we are running on a pretty big WP database with a very large posts table with ~500k entries in the posts table. When going to the front page of the blog a query to the wp_posts table is made during WP initialization. This default query on the big table takes ~1sec. The data returned by this query is not required afterwards, so I'd like to avoid doing it.
>
> In wp->main() the query_posts() is always executed and I don't seem to have very much control of what is queried in there. I'd like to avoid querying anything at this stage at all. Later on in the page flow I'll query the database differently. Any idea?
Hi Martin,
Yes, there are unfortunately no hooks to disable running of the first query. I seem to remember it being one of my first trac tickets (though I can't find it now) though it didn't get any traction.
For the longest time I was at a lost for how to resolve the issue without hacking core and recently I think I identified a solution. If you subclass the WP class you can bypass the get_posts() function for the root, or call the standard one otherwise. Here's some code to try.I haven't fully tested it but it should get you started:
class WP2 extends WP {
static function on_load($extra_query_vars = '') {
add_action('setup_theme',array(__CLASS__,'action_setup_theme'));
}
static function action_setup_theme() { // Setup theme is the first code run after WP is created.
global $wp;
$wp = new WP2(); // Replace the global $wp
}
function get_posts($query_args = '') {
list($path) = explode('?',$_SERVER['REQUEST_URI']);
if ($path=='/')
return array();
else
return parent::get_posts($query_args); // Delegate to WP class
}
}
WP2::on_load();
Hope this helps.
-Mike
P.S. Let me know how it goes...
More information about the wp-hackers
mailing list