2

I'm starting work on a legacy site, that has gotten quite messy. We'd sure like to clean things up. The site has 3914 page entries and with 27 different page templates. I kid you not.

One thing that would really help, and that's if we could list all the pages (by title) WITH THEIR ACCOMPANYING TEMPLATE NAME.

I'm well aware of "list all" type of plugins. All of them use the WordPress core function <?php wp_list_pages( $args ); ?> Unfortunately that function doesn't give much freedom for selecting different elements from within the page.

My tendency is to evaluate that core function, and recreate my own including template name, certainly no easy task.

Anybody been here before? Many thanks.

Update: Based on the suggestion of Howdy_McGee I've been attempting to use WordPress core functions to do this. My code: <?php echo "<ul>"; $query = new WP_Query(); // var_dump($query); while ( $query->have_posts() ) : $query->the_post(); $template_name = get_page_template_slug(); ?> <li>
<?php the_title(); ?> <?php echo " | Template File: ".$template_name; ?> </li> <?php endwhile; ?> </ul>

and that's been a fail. $query returns an empty set. I've been trying to figure out the correct $args.

Edit #2: And then there is Alexey answer. Wow. No really WOW! I'm blown away by that. Never thought about going directly to the database. You've given me a whole new approach to thinking about things on this WordPress stuff. Many thanks!

  • Honestly, I would create one more page template so you can page the results using WP_Query and get_page_template_slug(). I would write a formal answer but I'm short on time, if one doesn't appear or you don't come across the answer I'll submit one later. – Howdy_McGee Jan 22 '15 at 23:00
2

Not so hard as you expected. Try this:

global $wpdb;

$sql = "SELECT post_title, meta_value
  FROM $wpdb->posts a
  JOIN $wpdb->postmeta b ON a.ID = b.post_id
  WHERE a.post_type = 'page'
  AND a.post_status = 'publish'
  AND b.meta_key = '_wp_page_template'
        ";

$pages = $wpdb->get_results($sql);

echo '<pre>';
print_r($pages);
echo '</pre>';
| improve this answer | |
  • Alexey, you are my hero for this answer. Many thanks! – zipzit Jan 23 '15 at 1:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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