1

I need to get all visible (published) pages and then determine which template they use. I need to do this in standard WordPress API not via the REST API.

Is there a function that can do that?

2
  • you can get that with codex.wordpress.org/Function_Reference/get_posts
    – mmm
    Apr 18, 2018 at 8:12
  • You can simply view them from the body classes. If you need a programmatic solution, you can print the global $template variable in footer. That will print the path to the current file being used in footer.
    – Abhik
    Apr 18, 2018 at 8:38

1 Answer 1

1

You can use get_pages to get all the published pages, which is similar to get_posts, as

<?php $args = array(
    'sort_order' => 'asc',
    'sort_column' => 'post_title',
    'hierarchical' => 1,
    'exclude' => '',
    'include' => '',
    'meta_key' => '',
    'meta_value' => '',
    'authors' => '',
    'child_of' => 0,
    'parent' => -1,
    'exclude_tree' => '',
    'number' => '',
    'offset' => 0,
    'post_type' => 'page',
    'post_status' => 'publish'
); 
$pages = get_pages($args); // get all pages based on supplied args

foreach($pages as $page){ // $pages is array of object
   $page_template = get_post_meta($page->ID, '_wp_page_template', true); // Page template stored in "_wp_page_template"

   echo $page_template;
}

?>

Checkout these links for more detail:

get pages: https://codex.wordpress.org/Function_Reference/get_pages

2022.06.18 - This page has been moved to https://developer.wordpress.org/reference/functions/get_pages/

get page template: https://codex.wordpress.org/Function_Reference/get_page_template

2022.06.18 - This page has been moved to https://developer.wordpress.org/reference/functions/get_page_template/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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