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'
));

if(!empty($pages))
{
    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);

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


UpChecker

Reliable uptime monitoring and instant alerts for any website downtime.

  • Uptime Monitoring
  • Performance Monitoring
  • SSL Certificate Alerts
  • Domain Monitoring
  • DNS Checker
  • XML Sitemap Monitoring