Title: WP_Query inside functions.php
Last modified: August 19, 2016

---

# WP_Query inside functions.php

 *  Resolved [billygates](https://wordpress.org/support/users/billygates/)
 * (@billygates)
 * [16 years, 6 months ago](https://wordpress.org/support/topic/wp_query-inside-functionsphp/)
 * Hello,
 *  there is a custom WP theme. The directory with the theme also contains _function.
   php_ file where are stored frequently used theme shortcuts. _index.php_ of this
   theme shows the recent posts via the global loop. Also I want to show on _index.
   php_ links to some static pages which meet some condition.
 * To do that I create a function in _functions.php_ like this:
 *     ```
       function print_custom_pages($tp)
       {
         $query = new WP_Query("post_type=page&meta_key=pg_type&meta_value=$tp");
         while ($query->have_posts()) {
            /* echo "POST found<br />\n"; */  // is not printed
            $query->the_post();
            the_permalink(); // print a permalink of the page
         }
       }
       ```
   
 * but it does not work: no posts are found via WP_Query. What I am doing wrong?
 * Thanks.

Viewing 3 replies - 1 through 3 (of 3 total)

 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [16 years, 6 months ago](https://wordpress.org/support/topic/wp_query-inside-functionsphp/#post-1311021)
 * Try:
 * `$query = new WP_Query('post_type=page&meta_key=pg_type&meta_value=' . $tp');`
 *  Thread Starter [billygates](https://wordpress.org/support/users/billygates/)
 * (@billygates)
 * [16 years, 6 months ago](https://wordpress.org/support/topic/wp_query-inside-functionsphp/#post-1311040)
 * [@esmi](https://wordpress.org/support/users/esmi/): does not work, as far as 
   understand there is no difference, just an alternative way to write the same 
   thing.
 *  Thread Starter [billygates](https://wordpress.org/support/users/billygates/)
 * (@billygates)
 * [16 years, 6 months ago](https://wordpress.org/support/topic/wp_query-inside-functionsphp/#post-1311047)
 * question is closed. the problem was in ‘Exclude Pages’ plugin. Thanks to all 
   for your time.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘WP_Query inside functions.php’ is closed to new replies.

## Tags

 * [index.php](https://wordpress.org/support/topic-tag/index-php/)
 * [loop](https://wordpress.org/support/topic-tag/loop/)
 * [wp_query](https://wordpress.org/support/topic-tag/wp_query/)

 * 3 replies
 * 2 participants
 * Last reply from: [billygates](https://wordpress.org/support/users/billygates/)
 * Last activity: [16 years, 6 months ago](https://wordpress.org/support/topic/wp_query-inside-functionsphp/#post-1311047)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
