How to Query Posts in WordPress: Simple Guide with Examples
To query posts in WordPress, use the
WP_Query class by passing an array of parameters like 'post_type' and 'posts_per_page'. Then loop through the results with have_posts() and the_post() to display each post.Syntax
The WP_Query class is used to fetch posts based on parameters.
- post_type: Type of content to query (e.g., 'post', 'page').
- posts_per_page: Number of posts to retrieve.
- category_name: Filter posts by category slug.
- orderby: How to sort posts (e.g., 'date', 'title').
- order: Sorting direction ('ASC' or 'DESC').
After creating the query, use have_posts() to check if posts exist and the_post() to set up each post in the loop.
php
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => 5,
'category_name' => 'news',
'orderby' => 'date',
'order' => 'DESC'
);
$query = new WP_Query($args);
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
// Display post content here
}
wp_reset_postdata();
} else {
// No posts found
}
?>Example
This example fetches the latest 3 blog posts and displays their titles and excerpts.
php
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => 3
);
$query = new WP_Query($args);
if ($query->have_posts()) {
echo '<ul>';
while ($query->have_posts()) {
$query->the_post();
echo '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a><br>';
echo get_the_excerpt() . '</li>';
}
echo '</ul>';
wp_reset_postdata();
} else {
echo 'No posts found.';
}
?>Output
<ul><li><a href="https://example.com/post1">Post Title 1</a><br>Excerpt of post 1...</li><li><a href="https://example.com/post2">Post Title 2</a><br>Excerpt of post 2...</li><li><a href="https://example.com/post3">Post Title 3</a><br>Excerpt of post 3...</li></ul>
Common Pitfalls
- Not calling
wp_reset_postdata()after the loop can cause conflicts with other queries. - Using
query_posts()instead ofWP_Queryis discouraged because it overrides the main query. - Forgetting to check
have_posts()before looping leads to errors. - Passing incorrect parameter names or values results in empty results.
php
<?php // Wrong way: Using query_posts (legacy and can cause issues) query_posts(array('posts_per_page' => 2)); if (have_posts()) { while (have_posts()) { the_post(); the_title(); } } wp_reset_query(); // Right way: Using WP_Query $args = array('posts_per_page' => 2); $query = new WP_Query($args); if ($query->have_posts()) { while ($query->have_posts()) { $query->the_post(); the_title(); } wp_reset_postdata(); } ?>
Quick Reference
| Parameter | Description | Example Value |
|---|---|---|
| post_type | Type of content to query | 'post', 'page', 'custom_post_type' |
| posts_per_page | Number of posts to retrieve | 5, 10, -1 (all) |
| category_name | Filter by category slug | 'news', 'events' |
| orderby | Sort posts by field | 'date', 'title', 'rand' |
| order | Sort direction | 'ASC' or 'DESC' |
Key Takeaways
Use WP_Query with an array of parameters to fetch posts flexibly.
Always check have_posts() before looping and call wp_reset_postdata() after.
Avoid using query_posts() as it can break the main query.
Parameters like post_type and posts_per_page control what posts you get.
Use get_the_title(), get_the_excerpt(), and get_permalink() inside the loop to display post info.