I want to know if it's possible to get the ID of a page with a specific template. Is this possible to get the ID of a page that assigned to "page-special.php"?

up vote 31 down vote accepted

When a page is created, the assigned template to that page is saved as custom post meta in the same way as custom fields. The meta_key is _wp_page_template and the meta_value will be the page template

You can simply make use of get_pages to retrieve all pages which have a meta_value of the specified template

$pages = get_pages(array(
    'meta_key' => '_wp_page_template',
    'meta_value' => 'page-special.php'
));
foreach($pages as $page){
    echo $page->ID.'<br />';
}

EDIT 23-07-2015

If one just needs the page ids, then you make use of get_posts and then just pass page as post_type and 'idsasfields` value. This will ensure a much faster, much more optimized query as we will only return the post id column in the db and not all of them for the given pages

(Requires PHP 5.4+)

$args = [
    'post_type' => 'page',
    'fields' => 'ids',
    'nopaging' => true,
    'meta_key' => '_wp_page_template',
    'meta_value' => 'page-special.php'
];
$pages = get_posts( $args );
foreach ( $pages as $page ) 
    echo $page . '</br>';
  • Hey, Thanks. Isn't a bit too "heavy"? (running through all the pages) – user3800799 Nov 2 '14 at 8:29
  • Depends on how many pages you have. There is actually no faster native way that I know of to retrieve this data. If you have a lot of pages, I would suggest that you make use of transients to store that data and only flush/delete the transient when a new page is published – Pieter Goosen Nov 2 '14 at 8:35
  • My pleasure, glad I could help. Enjoy :-) – Pieter Goosen Nov 2 '14 at 8:44
  • @user3800799 I have updated the post if you are only interested in getting the ids, nothing else – Pieter Goosen Jul 23 '15 at 10:47
  • You could also use set_transient (codex.wordpress.org/Transients_API) if you don't want to query the database too much. – Chris Andersson May 19 '16 at 12:09

If your page template resides inside sub-folder, theme-folder/page-templates/page-template.php then you below query will works:

$page_details = get_pages( array(
 'post_type' => 'page',
 'meta_key' => '_wp_page_template',
 'hierarchical' => 0,
 'meta_value' => 'page-templates/page-template.php'
));

This above codes also display sub-pages as well.

Thanks

The following is a slightly more articulated script that take into account a language, if needed. NOTE that it assumes the usage of Polylang, not WPML.

function get_post_id_by_template($template,$lang_slug = null){
  global $wpdb;
  $wh = ($lang_slug) ? " AND t.slug = %s" : "";

  $query = $wpdb->prepare(
    "SELECT DISTINCT p.ID
    FROM $wpdb->posts p
    INNER JOIN $wpdb->postmeta meta ON meta.post_id = p.ID
    INNER JOIN $wpdb->term_relationships tr ON meta.post_id = tr.object_id
    INNER JOIN $wpdb->term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
    INNER JOIN $wpdb->terms t ON tt.term_id = t.term_id
    WHERE p.post_status = 'publish' AND meta.meta_key = %s AND meta.meta_value = %s" . $wh,
    '_wp_page_template',
    $template,
    $lang_slug
  );

  $ids = $wpdb->get_results($query);

  if($ids && isset($ids[0])){
    $p = $ids[0];
    return $p->ID;
  } else {
    return false;
  }
}// get_post_id_by_template

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.