Skip to content
Merlin Tools

WP_Query builder

A query and its loop, built from the fields.

Genuinely free. No sign-up, no email, no limits, no cookies. We don't store the URL you analyse.

Code
<?php
$q = new WP_Query( array(
	'post_type'      => 'post',
	'posts_per_page' => 10,
	'orderby'        => 'date',
	'order'          => 'DESC',
) );

if ( $q->have_posts() ) :
	while ( $q->have_posts() ) : $q->the_post(); ?>
		<article <?php post_class(); ?>>
			<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
			<?php the_excerpt(); ?>
		</article>
	<?php endwhile;
	wp_reset_postdata();
else :
	echo '<p>No results.</p>';
endif;

What it's for

WP_Query is the heart of custom queries in WordPress, but its arguments are many and easy to forget. Here you set the main parameters and get the query plus a ready loop, with the final wp_reset_postdata() everyone forgets.

← All WordPress tools

Frequently asked questions

What is wp_reset_postdata() for?

It restores the global post after a custom loop. If you forget it, the rest of the page can show the wrong data. This builder always includes it.