Title: Using functions.php to do the custom queries
Last modified: August 20, 2016

---

# Using functions.php to do the custom queries

 *  [SpiritOfTheWinds](https://wordpress.org/support/users/spiritofthewinds/)
 * (@spiritofthewinds)
 * [14 years, 8 months ago](https://wordpress.org/support/topic/using-functionsphp-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

Viewing 1 replies (of 1 total)

 *  [Rev. Voodoo](https://wordpress.org/support/users/rvoodoo/)
 * (@rvoodoo)
 * [14 years, 8 months ago](https://wordpress.org/support/topic/using-functionsphp-to-do-the-custom-queries/#post-2321710)
 *     ```
       // SORT OUR MEMBERS PAGE
       function voodoo_member_sort(&$query) {
               if (is_post_type_archive( 've_members' ) ){
               $query->set('order', 'ASC');
            }
       }
       add_action('pre_get_posts', 'voodoo_member_sort');
       ```
   
 * is how I sort from funtions.php
 * As for different page templates…. maybe use is_page_template( ‘stuff.php’ );
 * [http://codex.wordpress.org/Function_Reference/is_page_template](http://codex.wordpress.org/Function_Reference/is_page_template)

Viewing 1 replies (of 1 total)

The topic ‘Using functions.php to do the custom queries’ is closed to new replies.

## Tags

 * [add_filter](https://wordpress.org/support/topic-tag/add_filter/)
 * [query](https://wordpress.org/support/topic-tag/query/)
 * [query_posts](https://wordpress.org/support/topic-tag/query_posts/)
 * [request](https://wordpress.org/support/topic-tag/request/)
 * [template](https://wordpress.org/support/topic-tag/template/)

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 1 reply
 * 2 participants
 * Last reply from: [Rev. Voodoo](https://wordpress.org/support/users/rvoodoo/)
 * Last activity: [14 years, 8 months ago](https://wordpress.org/support/topic/using-functionsphp-to-do-the-custom-queries/#post-2321710)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
