330 Locust Dr.
Verona, WI 53593
Powderkeg Web Design
Message Us

Web Dev Tid Bit (WDTB): Querying Pages By Template

July 07, 2017

Here is something I find myself looking up every now and then to get the syntax right / remember how to do. You may run in to a situation where you want to get a list of all pages that have a certain template in WordPress. There is a built in way for you to find these using the get_posts() function:

This tells WordPress to “Get me all pages that have been published that use the tpl-my-template template. It is specifically looking for the meta_key of “_wp_page_template”, which all pages should have set. I’ve found use cases for this in places where I want to showcase certain top level pages, multiple galleries, etc. You can get a lot more specific with your query as well with things like looking only for pages with no parents, pages that have a specific meta field, pages that are also categorized with a specific tag, etc.


Lead Developer

Nick is an ambitious and versatile developer with a wide range of talents. He is an efficient coder that utilizes his resources in order to achieve both his and the clients’ goals. His Alma Mater is the University of Wisconsin – Stevens Point where he graduated with honors in Web and Digital Media Development.