Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress query pages with certain template

Tags:

wordpress

Is there a way to query Wordpress pages that have certain template? Here's what I got, but doesn't show anything:

<?php $my_query = new WP_Query(array( 'meta_key' => '_wp_page_template', 'meta_value' => 'template-city.php' )); ?>
                <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
                    <li>
                    <?php the_title(); ?>
                    </li>
                <?php endwhile; ?>

Of course, there is page template file named template-city.php

like image 322
jOpacic Avatar asked Aug 23 '12 07:08

jOpacic


1 Answers

if post_type is left out WP will look for post, and you are looking for pages.

<?php
    $args = array(
        'post_type' => 'page',//it is a Page right?
        'post_status' => 'publish',
        'meta_query' => array(
            array(
                'key' => '_wp_page_template',
                'value' => 'template-city.php', // template name as stored in the dB
            )
        )
    );
$my_query = new WP_Query($args)
?>
like image 177
janw Avatar answered Nov 04 '22 11:11

janw