Get all pages IDs that use specific template

We have a big site. And we have challenge edit some template, but also we need see our changes for all pages. As a rule, templates may be used for many pages. And we should be get links for all pages. How this could be done? About that below.

How to get all pages IDs that use specific template

Problem: How to get all pages IDs that use specific template

Solution: Let's write a simple function for resolve this challenge

<?php

function get_pages_IDs_that_use_template( $template_path ) {
	$args = [
		'post_type' => 'page',
		'fields' => 'ids',
		'no_found_rows' => true,
		'meta_key' => '_wp_page_template',
		'meta_value' => $template_path, // path to our template e.g. 'pages/tpl-our-impact.php'
	];
	
	$pages_ids = new WP_Query( $args );
	// If we should be get links uncomment next string and comment 'return $pages_ids;' string
	/* echo '<h3>Lists links:</h3><ul>';
	   foreach ( $pages_ids as $page_id ) {
	       echo '<li><a href="' . get_permalink( $page_id ) . '">' . get_permalink( $page_id ) . '</a></li>;
		}
	echo '</ul>';*/
	
	return $pages_ids;
}

Leave Comment

Your email address will not be published.