WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'd like to query only pages with a certain page template with WP_Query or a function that would return the post object, but I can't find any information about that on the officiel codex.

share|improve this question
up vote 7 down vote accepted

Try this... Assuming the template name is 'my_template.php',

$query = new WP_Query( array( 'meta_key' => '_wp_page_template', 'meta_value' => 'my_template.php' ) );
//Down goes the loop...

You can also use get_posts, or modify query posts to get the job done. Both these functions use the same parameters as WP_Query.

share|improve this answer

Incorrect: as of wordpress 3 you need something akin to:

$args = array(
    'post_type'  => 'page', 
    'meta_query' => array( 
        array(
            'key'   => '_wp_page_template', 
            'value' => 'my_template.php'
        )
    )
);
share|improve this answer
    
THANK YOU!! Skimmed this page way too quickly, used the accepted answer. This does it. For anyone else, note the array inside the array inside the array... – Jeremy Carlson Apr 26 at 22:40

The page template is stored as a meta value with key "_wp_page_template".

So all you need is to use that key in a meta query parameter. For examples

See http://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query#Query_based_on_Custom_Field_and_Sorted_by_Value

and http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.