[wp-hackers] Avoid query_post on frontpage on wp initialization

Martin Widmann widmann.martin at gmail.com
Tue Dec 28 05:19:47 UTC 2010


Hi Mike,

thank you very much for the code snippet. This looks like a very elegant solution to me. I'll try this out ASAP and tell you what happened!

Martin

On 27.12.2010, at 22:33, Mike Schinkel wrote:

> 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...
> _______________________________________________
> 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