Home >Backend Development >PHP Tutorial >Powerful query_posts() function in wordpress_PHP tutorial
Today I will talk about the main query function of WordPress -query_posts(), because I have used this function many times in the theme I am making.
The query_posts() query function determines which posts appear in the WordPress main loop. Because of this, the query_posts function is only used to modify the home page loop (Loop), rather than generating secondary loops on the page. If you want to generate additional loops outside the main loop, you should create new independent WP_Query objects and use these objects to generate loops. Using query_posts on a loop outside of the main loop will cause the main loop to run skewed and may display content on the page that you don't want to see.
The query_posts() query function receives a large number of parameters, and the format is the same as the parameter format in the URL (for example, p=4 means the article with ID 4). Here are some examples of some commonly used syntax formats of the query_posts function.
Add the following code to the index.php file so that the articles displayed on the homepage can come from any category except category 3.
Php codeYou can also exclude more categories.
Php codeUse the following statement to retrieve a specified article:
Php codeIf you want to use the Read More function in the query statement, please set the global variable $more to 0.
Php codeUse the following statement to retrieve a specified page:
Php codeor
Php codeWhen retrieving a subpage, you need to provide the alias of the subpage and its parent page, separating the two with a slash. For example:
Php codeThe above methods all use the form of query_posts($query_string) to call this function. Here is another method to pass parameter variables using an array.
Php codeCompared with the string method, the array form is more intuitive and less error-prone.
Below are some frequently used parameters. Some of them have been used by me, and some have not been used. Let’s just summarize them.
Only show articles under specific categories.
Display a single category based on ID
Only display articles from a certain category ID (and subcategories under this category):
Php codeDisplay a single category based on category name
Only display articles from a certain category name:
Php codeShow multiple categories based on ID
Display articles from several specified category IDs:
Php codeExclude articles in a certain category
Displays all articles except articles in a certain category. Excluded category IDs are prefixed with a minus sign ('-').
Php codeThe above code deletes articles in the category with ID 3.
Handle multiple categories
Display articles belonging to multiple categories. The following code can display articles belonging to both category 2 and category 6:
Php codeIf you want to display articles in category 2 or category 6, you can use the cat introduced above, or you can use the category_in function (note that articles in subcategories under the category will not be displayed here):
Php codeYou can exclude articles in multiple categories in the following way:
Php codeDisplay articles under a specific tag.
Get articles in a certain tag
Php codeGet articles in any of several tags
Php codeMultiple tags
Display articles belonging to tags with IDs 37 and 47:
Php codeTo display articles under tags with ID 37 or 47, you can use the tag parameter or tag_in:
Php codeThe displayed article belongs to neither tag 37 nor tag 47:
Php codetag_slug_in works almost the same as tag_slug_and, except that the matching aliases are different.
You can also select articles based on their authors.
Note: author_name operates on the user_nicename field, while author operates on the author id field.
Display all pages published by the author with ID 1, arrange the pages in title order, and there is no pinned article above the page list:
Php codeRetrieve a single article or page.
The pinned article feature was introduced in WordPress 2.7. In the query, the article set as "top" will be displayed before other articles, unless the article has been excluded by the caller_get_posts=1 parameter.
Return to the first pinned article
Php codeor
Php codeNote: The second method can only return the latest pinned article; if there is no pinned article currently, return the latest published article.
Return to the first pinned article; if there is none, no content will be returned
Php codeExclude all pinned articles from the query
Php codeReturns all articles under a certain category, but does not display pinned articles above the article list. All articles set to "pinned" are displayed in normal order (such as date order)
Php codeReturn to all articles under a certain category, do not display pinned articles at all, and retain paging
Php codeSearch for articles published within a specific time period.
Return to recently published articles
Php codeReturn to the article published on December 20th
Php codeReturn to articles published between March 1st and March 15th, 2009
Php codeReturn to articles published in the last 30 days
Php codeReturn to articles published in the past 30 days to the past 60 days
Php codeThrough the offset parameter, you can remove or ignore one or more initial articles in the query set under normal circumstances.
The following shows the 5 posts following the most recent post:
Php codeDecide whether to sort in ascending or descending orderSort parameter
Retrieve articles (or pages) based on custom keywords or values.
Return articles with the keyword 'color' and the value 'blue':
Php codeReturn articles with the custom field keyword 'color', regardless of the custom field value:
Php codeReturn articles with custom field value 'color', regardless of keyword:
Php codeReturn pages where the custom field value is 'green', regardless of the custom field keyword:
Php codeReturn articles and pages whose custom keyword is 'color' and whose custom field value is not 'blue':
Php codeReturns articles whose custom field keyword is 'miles' and the custom field value is less than or equal to 22. Note that a field value of 99 will be considered greater than a field value of 100 because the data is stored as a string rather than a number.
query_posts('meta_key=miles&meta_compare=<=&metavalue=22');
As you may have seen from some of the examples above, you can use the & symbol to connect different parameters, such as:
Php codeDisplay articles on the homepage published in the current month and belonging to category 13:
Php codeIn WP 2.3, the following parameter union returns two articles that belong to both category 1 and category 3, in descending order by article title:
Php codeIn WP 2.3 and WP 2.5, the following parameter combination should return articles belonging to category 1 with the "apples" tag:
Php codeBut due to a bug, the code failed to show normal results. There is a solution: use the + sign to find multiple tags:
Php codeThis shows the results we want to show.
The "Maximum blog pages to display" parameter in Settings > Reading will affect your query results. To override the setting in Settings > Reading, you need to add the 'posts_per_page' parameter to the tag. For example:
Php code
Note: The query_posts function will rewrite and replace the main query of the page. As a precaution, please do not use query_posts for other purposes.
Source: http://www.zuluo.net/2012/2012-01/wordpress-query_posts.html