in WordPress

List Of Pages Using A Page Template

There are a few differences between using pages or posts, one of the biggest difference between these post types are that pages can be assigned a page template which allow you to completely change the layout of the page. When you assign a new page template to a page this template will be stored onto the post meta data, WordPress will then search for this meta data field and use the correct template to display your page. Because it stores this value in a post meta data we can query the database to return all pages which have been assigned a certain page template. There are a few ways we can query the database for pages with a certain meta information, we can use the get_posts() function or we can use the WP_Query class. Using the get_posts() function you just need to pass in the meta key and meta value we want to search on, below is the code to use when using the get_posts() function all you have to do is replace the page-template.php with the template file you want to search for..

$pages = get_pages(array(
    'meta_key' => '_wp_page_template',
    'meta_value' => 'page-template.php'

    foreach($pages as $page){
    echo $page->post_title.'<br />';

If you want to use the WP_Query class you can do this by using the following code.

$args = array(
            'post_type' => 'page',
            'meta_key' => '_wp_page_template',
            'meta_value' => 'page-templates/tools-page.php'
$page_template_query = new WP_Query($args);

    foreach($page_template_query->posts as $page){
    echo $page->post_title.'<br />';

Upgrade to access all content on Paulund

Members unlock all tutorials and snippets

Access to all downloadable content

Access to code examples before others

Sign Up Now

Already a member? Login here

Subscribe To The Weekly Newsletter

Get weekly updates to your email