How to get posts by page template in WordPress
May 15, 2015
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!