Skip to content

Instantly share code, notes, and snippets.

@brunomonteiro3
Created September 26, 2016 18:23
Show Gist options
  • Save brunomonteiro3/5aa78d2abb4fe8c459c34f841b6b45c4 to your computer and use it in GitHub Desktop.
Save brunomonteiro3/5aa78d2abb4fe8c459c34f841b6b45c4 to your computer and use it in GitHub Desktop.
Query posts with specific template
<?php
/*
Template Name: List
*/
?>
<style>
td, tr{
border: 1px solid grey;
padding: 10px;
}
</style>
<h1>
Lista:
</h1>
<table>
<tbody>
<thead>
<tr>
<td>Title</td>
<td>Link</td>
</tr>
</thead>
<?php
// WP_Query arguments
$args = array (
'post_type' => 'page',
'posts_per_page' => 1000,
'meta_key' => '_wp_page_template',
'meta_value' => 'template_name.php'
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
?>
<tr>
<td><?php the_title(); ?></td>
<td><a href="<?php the_permalink(); ?>">Link</a></td>
</tr>
<?php
}
} else {
// no posts found
}
// Restore original Post Data
wp_reset_postdata();
?>
</tbody>
</table>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment