The query_posts function is used in the WordPress topic to control which articles can appear in the main loop. It may be said that many people do not understand the main loop. For example, the articles on the home page and archive page (including those on the page) are all in the main loop. When query_posts function control is not used, the homepage and archive page list all published articles on your blog based on the release time of the article. If you want to define which articles can be displayed, the query_posts function will be used if the document is not displayed and sorted by the method.
Disable or display articles of a certain category
If you do not want the articles of a category to appear in the main loop, you can use query_posts to add the category _ not_in parameter:
$ Paged = (get_query_var ('paged '))? Get_query_var ('paged'): 1;
$ Args = array (
// 2, 6 is the category ID you do not want to display, multiple are separated by commas
'Category _ not_in '=> array (2, 6 ),
'Paged' => $ paged
);
Query_posts ($ args );
If you only want to display an article of a specific category, you can change category _ not_in to category _ in. Similarly, if an article under a tag is not displayed, you can change category _ not_in to tag _ not_in or just want to display the article under a tag, you can change category _ not_in to tag _ in, followed by the tag ID.
How to sort articles
$ Paged = (get_query_var ('paged '))? Get_query_var ('paged'): 1;
$ Args = array (
// The title in the following code is the value of orderby, sorted by title
'Orderby' => title,
'Paged' => $ paged
);
Query_posts ($ args );
Different orderby values allow articles to be sorted in many ways. The following lists several common values and their corresponding sorting methods: title: By title; date: by release date; modified: by modification time; ID: by Article ID; rand: random sorting; comment_count: By comment
Show only the articles with the corresponding ID
For example, if you only want to display an article with the ID of 2, 4, and 6, you can use the following code:
$ Paged = (get_query_var ('paged '))? Get_query_var ('paged'): 1;
$ Args = array (
// In the following code, 2, 4, and 6 are the document IDs.
'Post _ in' => array (2, 4, 6 ),
'Paged' => $ paged
);
Query_posts ($ args );
If you do not want to display Articles 2, 4, and 6, you can change post _ in to post _ not_in. In addition, if you only want to display the top article, you can change array (2, 4, 6) to get_option ('sticky _ posts'). This code will automatically fill in the IDs of all the top posts.
Leave the top article unpinned
If you do not want the top posts to be displayed on the top, but want them to be installed in a normal order, you can use the following code:
$ Paged = (get_query_var ('paged '))? Get_query_var ('paged'): 1;
$ Args = array (
'Paged' => $ paged,
'Ignore _ sticky_posts' => 1
);
Query_posts ($ args );
List all status articles
There are many types of WordPress articles, including published, draft, deleted, private, and regularly published articles. If you want to display all these articles, you can:
$ Paged = (get_query_var ('paged '))? Get_query_var ('paged'): 1;
$ Args = array (
'Post _ status' => array ('Publish ', 'pending', 'Draft', 'Future ', 'private', 'trash '),
'Paged' => $ paged
);
Query_posts ($ args );
The post_status parameter can control the status of a specific article, including pending, publish, draft, future, and private ), trash (deleted ).
Control the number of articles
If you want to control the number of articles to be displayed, you can use the showposts parameter:
$ Paged = (get_query_var ('paged '))? Get_query_var ('paged'): 1;
$ Args = array (
// Only 10 articles are displayed. If you change 10 to-1, all articles are displayed.
'Showposts' => 10,
'Paged' => $ paged
);
Query_posts ($ args );
If you only want to control the number of articles displayed on each page, such as the home page and category page, you can go to the WordPress management background-set-read there to set the maximum number of articles displayed on the blog page.