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

2 Replies to “How to get all pages IDs that use specific template”

  • Jeanne Mceachern says:

    Great blog! Do you have any suggestions for aspiring writers?
    I’m planning to start my own blog soon but I’m a little lost on everything.
    Would you propose starting with a free platform like WordPress or go for a paid option? There are so many options out there that I’m totally confused ..
    Any ideas? Thank you!

  • Amparo Macdermott says:

    Pretty nice post. I just stumbled upon your blog and wanted to say that I’ve truly enjoyed browsing your blog posts. After all I will be subscribing in your rss feed and I’m hoping you write again soon!

Your email address will not be published. Required fields are marked *