query_posts()自定义文章查询方法

目录

描述

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:

Here is similar code using pre_get_posts in functions.php :

Note: The pre_get_posts action doesn't work for Page requests.

query_posts() 描述

用法

<?php
// The Query
query_posts$args );

// The Loop
while ( have_posts() ) : the_post();
    echo 
'<li>';
    
the_title();
    echo 
'</li>';
endwhile;

// Reset Query
wp_reset_query();
?>
Place a call to query_posts() in one of your Template files before The Loop begins. The wp_query object will generate a new SQL query using your parameters. When you do this, WordPress ignores the other parameters it receives via the URL (such as page number or category).

Preserving Existing Query Parameters

If you want to preserve the original query parameter information that was used to generate the current query, and then add or over-ride some parameters, you can use the $query_string global variable in the call to query_posts().

For example, to set the display order of the posts without affecting the rest of the query string, you could place the following before The Loop:

When using query_posts() in this way, the quoted portion of the parameter must begin with an ampersand (&).

Or alternatively, you can merge the original query array into your parameter array:

Combining Parameters

You may have noticed from some of the examples above that you combine parameters with an ampersand (&), like so:

Posts for category 13, for the current month on the main page:

At 2.3 this combination will return posts belong to both Category 1 AND 3, showing just two (2) posts, in descending order by the title:

The following returns all posts that belong to category 1 and are tagged "apples".

You can search for several tags using "+". In this case, all posts belong to category 1 and tagged as "apples" and "oranges" are returned.

query_posts() 用法

参数

Note: Parameter details can be found in the Parameter section of the WP_Query class article.

The examples below also work with the WP_Query object.

query_posts() 参数

示例

Exclude Categories From Your Home Page

Placing this code in index.php file will cause the home page to display posts from all categories except category ID 3.

You can also add some more categories to the exclude-list (tested with WP 3.3.1):

Retrieve a Particular Post

To retrieve a particular post, you could use the following:

Note: If the particular post is an attachment, you have to use attachment_id instead of p:

If you want to use the Read More functionality with this query, you will need to set the global $more variable to 0.

All Posts in a Category

The "Blog pages show at most" parameter in Settings > Reading can influence your results. To overcome this, add the 'posts_per_page' parameter. For example:

This will return ALL posts from the category.

However, for subcategories (or child categories), 'The Category Name' doesn't always work. Rather use 'category-slug' instead. See Function_Reference/is_category.

Syndication Feeds

The "Syndication feeds show the most recent" or 'posts_per_rss' parameters in Settings > Reading will overwrite any 'posts_per_page' parameter in a query used in a feed.

To overcome use (for example in a a custom ics feed, where all matching posts are required), use the "posts_limit" filter as follows:

Passing variables to query_posts

You can pass a variable to the query with several methods, depending on your needs. As with other examples, place these above your Loop:

Example 1

In this example, we concatenate the query before running it. First assign the variable, then concatenate and then run it. Here we're pulling in a category variable from elsewhere.

Example 2

In this next example, the double quotes tell PHP to treat the enclosed as an expression. For this example, we are getting the current month and the current year, and telling query_posts() to bring us the posts for the current month/year, and in this case, listing in ascending order so we get the oldest post at the top of the page.

Example 3

This example explains how to generate a complete list of posts, dealing with pagination. We can use the default $query_string telling query_posts() to bring us a full posts listing. We can also modify the posts_per_page query parameter from -1 to the number of posts you want to show on each page; in this last case, you'll probably want to use posts_nav_link() to navigate the generated archive.

Example 4

If you don't need to use the $query_string variable, another method exists that is more clear and readable, in some more complex cases. This method puts the parameters into an array. The same query as in Example 2 above could be done like this:

As you can see, with this approach, every variable can be put on its own line, for easier reading.

Example 5

It is even possible to use the array style (Example 4) to query multiple taxonomies. Simply supply the taxonomy slug with a string of comma-separated values (each value being one term). In the example below, we will get all movie posts starring either Bruce Campbell or Chuck Norris.

query_posts() 示例

历史

添加于 版本: 1.5.0

query_posts() 历史

源文件

query_posts() 函数的代码位于 wp-includes/query.php.

query_posts() 源文件

相关