Get pages by template name in WordPress

Quick post to show how to retrieve pages by certain custom template.

$pages = get_pages(array(
	'meta_key' => '_wp_page_template',
	'meta_value' => '<Insert your template file name here>'

Example of use

List all the published pages that use the template “page-quote.php”:

$pages = get_pages(array(
	'meta_key' => '_wp_page_template',
	'meta_value' => 'page-quote.php'
foreach($pages as $page){
	echo $page->post_title.'<br />';

How does it work?

The template assigned to the page is stored as a meta data by the key ‘_wp_page_template’. Using the get_pages() WordPress function, we can ask for the page where ‘_wp_page_template’ equals our template name.

More Info

get_pages() documentation

Hope this helps, post a comment if you have any questions.


Some people have been having problems with some pages not showing in the results. Thanks to Alyssa we have a solution for this problem. I’m posting it here for quick access:

The pages weren’t showing because they had another page as their parent. Alyssa solved this by adding ‘hierarchical’ => 0 to the query.

So the final code would be:

$pages = get_pages(array(
    ‘meta_key’ => ‘_wp_page_template’,
    ‘meta_value’ => ‘page-quote.php’,
    ‘hierarchical’ => 0
foreach($pages as $page){
    echo $page->post_title;
19 Comments Short URL , , , , ,

19 Responses to “Get pages by template name in WordPress”

  1. Huskie Boi July 5, 2011 at 7:18 pm #

    Totally AWESOME!!
    Thank you so much for sharing, you’re the best!!

  2. Jorge Pedret July 6, 2011 at 7:05 am #

    My pleasure! Happy to hear that is helping somebody :-) Cheers.

  3. Andrew December 4, 2011 at 9:56 pm #

    You helped me out, too… thank you!

  4. Alyssa February 10, 2012 at 8:05 pm #

    I put this code exactly as its shown, only replacing the page-quote.php with my own template file. But still the page is blank.

  5. Jorge Pedret February 10, 2012 at 9:40 pm #

    Hi Alyssa, could you post a link to your code and tell me what you’re trying to do.

    Make sure the template file is in the root of your template (i.e.: /wp-content/themes//page-template.php)
    Make sure there is at least one page created with that template assigned.
    Check for typos (stupid suggestion, I’m sure you already did) and that the code is being executed.

  6. Alyssa February 16, 2012 at 7:03 pm #

    Yes I am positive- the template is product.php and there are over 500 pages with this template

  7. Guillaume February 23, 2012 at 10:35 am #

    Works perfectly for me ! It’s exactly what I needed, thanks

  8. Alyssa March 1, 2012 at 9:13 pm #

    Hi I thought I replied to you before. The link is

    Actually It works with some page templates and not with others but I’m not sure why. The template that isn’t working is being used on over 200 published pages. I double checked spelling and everything is correct. I am completely baffled!

  9. Alyssa March 1, 2012 at 10:09 pm #

    wow I have figured out why it’s not working but cannot fix it… the templates that work are only the templates that have no parent page. But all the pages with this template need to have parent pages. Why does having a parent to the page prevent this code from working?

  10. Andy Adams March 24, 2012 at 12:34 am #

    Thanks for the tip, much appreciated :) .

  11. Alyssa April 11, 2012 at 5:42 pm #

    I figured out a fix to my problem, you have to add

    ‘hierarchical’ => 0

    So it would be:

    $pages = get_pages(array(
    ‘meta_key’ => ‘_wp_page_template’,
    ‘meta_value’ => ‘page-quote.php’,
    ‘hierarchical’ => 0
    foreach($pages as $page){
    echo $page->post_title.”;

  12. Jorge Pedret April 11, 2012 at 6:01 pm #

    Hey Alyssa, thanks for posting a solution for the problem :-) I’m happy you got it working.

  13. Jesper May 11, 2012 at 7:35 am #

    Thanx Jorge and Alyssa!
    ‘hierarchical’ => 0 was the extra magic

  14. James Revillini June 19, 2012 at 7:58 pm #

    Jorge, a suggestion: you might want to edit your post to indicate Alyssa’s discovery. I think it’s worth noting as I almost gave up on this solution until I read her notes. And thank you for the article!

  15. Jorge Pedret June 19, 2012 at 8:29 pm #

    Hey James, thanks for the suggestion. The post is updated now with Alyssa’s solution. Cheers!

  16. Alyssa August 9, 2012 at 3:28 pm #

    So glad I was able to help someone! Now another question- any idea how to add pagination to this?

  17. prasad October 22, 2012 at 5:50 am #

    Thanks for the solution……

  18. Simon November 30, 2012 at 10:28 pm #

    Thank You !!!


  1. Récupérer une page via son nom de template sous wordpress | Edouard Labre | Blog - July 12, 2012

    [...] à des difficultés pour récupérer une page grâce à son nom de template via la méthode get_pages(), le get_posts() suivant permet d’obtenir le même résultat.$pages = get_posts( array( [...]

Leave a Reply