21

I would 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 official codex.

5 Answers 5

27

UPDATE: This is now a decade old answer, meant for a very old version of WordPress. I can see the comments informing me that this might not work for newer WP versions, please do refer to the other answers below if mine is not working for your version of WP. For WP 2.*, this will work.

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

$query = new WP_Query(
    array(
        'post_type' => 'page',
        '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.

2
  • 1
    This is not up to date. See Guille Holmann's / BenJam's answer downstairs.
    – Nadav
    Commented Mar 29, 2021 at 10:01
  • Yes, it is an old answer, for an older version of WP. Very likely the internals have changed. My answer is from 2011, and theirs are from 2018/19. That's a long gap! Commented Apr 9, 2021 at 11:00
27

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'
        )
    )
);
4
  • 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... Commented Apr 26, 2016 at 22:40
  • 3
    The only difference here is the post_type. Otherwise you don't need the meta_query array for a single custom key/value pair. Commented Nov 16, 2016 at 6:21
  • Of course the meta query is needed. Except it could be "inline" with meta_key and meta_value or with a plain array, which could include multiple conditions. Commented Feb 14, 2020 at 16:04
  • The accepted answer is a decade old, for an older version. Doesn't make it incorrect, just outdated for newer versions of WP. I have updated my answer to specify this. Commented Apr 9, 2021 at 11:06
1

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

1

If you have the template inside another folder:

$args = array(
    'post_type' => 'page', //it is a Page right?
    'post_status' => 'publish',   
    'meta_query' => array(
        array(
            'key' => '_wp_page_template',
            'value' => 'page-templates/template-name.php', // folder + template name as stored in the dB
        )
    )
);
0

If anyone's attempt incorrectly results in zero posts, probably the template name is wrong. I tried the php file name and my template name and they didn't work. Then I decided to inspect the templates select box where we select the template on the page editor. I found this:

<option value="templates-map/component-tutorial-1.php" 
 selected="selected">Tutorial -1</option>

I used templates-map/component-tutorial-1.php and it worked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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