I want to list all site pages with the template they use.
Is there a mod to wp_list_pages which does this?
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It only takes a minute to sign up.
Sign up to join this communityhttps://codex.wordpress.org/Function_Reference/get_page_template_slug
a basic query to get all pages, sorted by title, then output page title and template file name:
$args = array(
'post_type' => array( 'page' ),
'order' => 'ASC',
'orderby' => 'title'
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<p>';
the_title();
echo ' - ';
echo get_page_template_slug();
echo '</p>';
}
wp_reset_postdata();
}
"posts_per_page" => -1
will show all of them, but I'd advise against that and instead set a high number such as 100 or 200 to prevent problems
If you wish to get a list of posts within all post types that are using a template, use:
$args = array(
'post_type' => 'any',
'order' => 'ASC',
'orderby' => 'title',
'posts_per_page' => -1
);
$query = new WP_Query($args);
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
if (get_page_template_slug($post->ID)) {
echo '<a href="' . get_the_permalink() . '">' . get_the_title() . ' - ' . esc_html(get_page_template_slug($post->ID)) . '</a><br>';
}
}
wp_reset_postdata();
}
Can be a large query though.
I have tried flowing code to get all page with template, and worked perfect for me. this code purpose was to get all custom page template,as i was developed single page template.
$args = array(
"post_type" => "page",
"order" => "ASC",
"orderby" => "menu_order"
);
$the_query = new WP_Query( $args );
if ( have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
?>
<div id="post-<?php the_ID() ?>">
<?php
global $post;
$slug = $post->post_name;
get_template_part("page", $slug);
?>
</div>
<?php
}
wp_reset_postdata();
}
get_template_part
to load a template
Rather than do this in PHP, you can do it with this mysql:
SELECT distinct meta_value FROM `wp_postmeta` WHERE meta_key='_wp_page_template'
I just needed it for a quick check so that I could clean up unused templates with the theme_page_templates
filter.
Using phpmyadmin (sql) you can run this query to get a list of all pages with its used template:
SELECT p.post_name, p.post_title, meta_key, meta_value FROM `wp_postmeta` INNER JOIN wp_posts p ON p.ID = post_id WHERE meta_key='_wp_page_template' ORDER BY meta_value, p.post_name
This is an old question, but for anyone coming here now:
You could use WP-CLI - https://wp-cli.org/
It might already be installed, depending on where you're working.
The relevant command is: https://developer.wordpress.org/cli/commands/post/list/
For example, if I want the pages:
wp post list --post_type=page --fields=name,url,page_tempalte
You could even get a CSV:
wp post list --post_type=page --fields=name,url,page_tempalte > pages.csv
wp_list_pages
or any part of WP Core is bad practice. This could be done usingWP_Query
, but a little context as to why you want to do this and what you hope to achieve by getting this would be useful. Of note, the Query Monitor plugin will list all templates used. Listing any sub-templates pulled in viaget_template_part
calls will be a much harder thing to do and require each page be loadedpage-$slug.php
andpage-$id.php