Using functions.php to do the custom queries
-
I’ve read about the fact that doing a query_posts inside a template slows down the loading quite a lot if the site is big since it do a query 2 times instead of 1. So I decided to change all my query_post($args) to a custom add_filer(‘request’ ‘alter_the_query’);
In WordPress Codex, I found this:
function alter_the_query( $request ) { $dummy_query = new WP_Query(); // the query isn't run if we don't pass any query vars $dummy_query->parse_query( $request ); // this is the actual manipulation; do whatever you need here if ( $dummy_query->is_home() ) $request['category_name'] = 'news'; return $request; } add_filter( 'request', 'alter_the_query' );But I can’t find any decent information on how I can adapt it.
Currently in my blog, I have this:
query_posts(array('post_type'=>'post', 'post_status'=>'publish', 'paged'=>$paged));
How can I translate this into the above code? Also, how can I verify the current template being viewed? Each template has a different query, so single.php would have be different than page.php or blog.php.I know there is is_home() or is_page() but the problem is that I have different templates for different pages, so I can’t use is_page().
Any ideas? Thanks
The topic ‘Using functions.php to do the custom queries’ is closed to new replies.