
For a recent project I wanted to get all pages by page template. Looking at the codex for WP_Query I wasn’t able to find anything for page templates. Digging further, I was able to use custom meta queries.
The meta query key is: _wp_page_template
The meta query value is the file name of the page template.
Here’s an example:
$pagesByPageTemplate = new WP_Query(array( 'post_type' => 'page', 'meta_query' => array( array( 'key' => '_wp_page_template', 'value' => 'page-custom_page_template.php' ) ), ));
That’s it! Now continue on with your WordPress loop!
Anthony Montalbano
If it's worth doing, it's worth doing right.
Published on: May 15, 2015
Last modified on: December 8, 2021