Codex
Codex tools: Log in
Attention Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!
Function Reference/query posts
Languages: English • Italiano • 日本語 • 中文(简体) • Português do Brasil • (Add your language)
Contents
1 Description
2 Caveats
2.1 Alters Main Loop
2.2 Secondary Loops
2.3 Pagination
2.4 Additional SQL Queries
3 Usage
3.1 Preserving Existing Query Parameters
3.2 Combining Parameters
4 Parameters
5 Examples
5.1 Exclude Categories From Your Home Page
5.2 Retrieve a Particular Post
5.3 All Posts in a Category
5.4 Syndication Feeds
5.5 Passing variables to query_posts
5.5.1 Example 1
5.5.2 Example 2
5.5.3 Example 3
5.5.4 Example 4
5.5.5 Example 5
6 Change Log
7 Source File
8 Resources
9 Related
9.1 Articles
9.2 Code Documentation
Description
Note: This function isn't meant to be used by plugins or themes. As explained later, there are better, more performant options to alter the main query. query_posts() is overly simplistic and problematic way to modify main query of a page by replacing it with new instance of the query. It is inefficient (re-runs SQL queries) and will outright fail in some circumstances (especially often when dealing with posts pagination). Any modern WP code should use more reliable methods, like making use of pre_get_posts hook, for this purpose.
WP_Query, query_posts and get_posts use cases explained
query_posts() is a way to alter the main query that WordPress uses to display posts. It does this by putting the main query to one side, and replacing it with a new query. To clean up after a call to query_posts, make a call to wp_reset_query(), and the original main query will be restored.
It should be noted that using this to replace the main query on a page can increase page loading times, in worst case scenarios more than doubling the amount of work needed or more. While easy to use, the function is also prone to confusion and problems later on. See the note further below on caveats for details.
For general post queries, use WP_Query or get_posts
It is strongly recommended that you use the pre_get_posts filter instead, and alter the main query by checking is_main_query
For example, on the homepage, you would normally see the latest 10 posts. If you want to show only 5 posts (and don't care about pagination), you can use query_posts() like so:
query_posts( 'posts_per_page=5' );
Here is similar code using pre_get_posts in functions.php :
function five_posts_on_homepage( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
$query->set( 'posts_per_page', 5 );
}
}
add_action( 'pre_get_posts', 'five_posts_on_homepage' );
Note: The pre_get_posts action doesn't work for Page requests.
Caveats
query_posts() is only one way amongst many to query the database and generate a list of posts. Before deciding to use query_posts(), be sure to understand the drawbacks.
Alters Main Loop
query_posts() is meant for altering the main loop. It does so by replacing the query used to generate the main loop content. Once you use query_posts(), your post-related global variables and template tags will be altered. Conditional tags that are called after you call query_posts() will also be altered - this may or may not be the intended result.
Secondary Loops
To create secondary listings (for example, a list of related posts at the bottom of the page, or a list of links in a sidebar widget), try making a new instance of WP_Query or use get_posts().
If you must use query_posts(), make sure you call wp_reset_query() after you're done.
Pagination
Pagination won't work correctly, unless you set the 'paged' query var appropriately: adding the paged parameter
Additional SQL Queries
If you use query_posts within a template page, WordPress will have already executed the database query and retrieved the records by the time it gets to your template page (that's how it knew which template page to serve up!). So when you over-ride the default query with query_posts(), you're essentially throwing away the default query and its results and re-executing another query against the database.
This is not necessarily a problem, especially if you're dealing with a smaller blog-based site. Developers of large sites with big databases and heavy visitor traffic may wish to consider alternatives, such as modifying the default request directly (before it's called). The request filter can be used to achieve exactly this.
The 'parse_query' and the 'pre_get_posts' filters are also available to modify the internal $query object that is used to generate the SQL to query the database.
Usage
In
Marketing Top 5 Reasons You’re Not Seeing ROI from Social Media
In
Marketing 3 Simple Steps to Stay On Track with Your Marketing This Year