tools/wordpress-wp-query-loop-generator
WordPress Query Loop Generator
Build a secondary WP_Query and the loop that goes with it — filters, ordering, meta and working pagination, with the wp_reset_postdata() call that keeps the rest of the page correct.
snippet.php
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 10,
'orderby' => 'date',
'order' => 'DESC',
'ignore_sticky_posts' => true,
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
echo '<ul>';
while ( $query->have_posts() ) {
$query->the_post();
printf(
'<li><a href="%s">%s</a></li>',
esc_url( get_permalink() ),
esc_html( get_the_title() )
);
}
echo '</ul>';
// Puts the global $post back to the main query's post, which the_post()
// overwrote. Without this every template tag after the loop — the page
// title, the comment template, anything calling get_the_ID() — reads the
// last post looped over here instead.
wp_reset_postdata();
} else {
printf( '<p>%s</p>', esc_html__( 'Nothing found.', 'text-domain' ) );
}