WordPress powerful invocation of the article function query posts usage

Source: Internet
Author: User

Query posts is a very useful invocation of the article function, can be done with the page to display a variety of specific range of articles, such as can invoke a classification, tags, dates and authors, and other different ranges of the article list. The list of these articles can greatly enrich the content of WordPress pages and facilitate SEO. Second-hand scientists have collated the functions of the following query posts call articles, respectively.

The first is the general wording of query posts. This is usually the case when you define a query and then add the article Loop code before resetting the query.

<?php//define the range of articles to be displayed query Query_posts (); Article Loop if (Have_posts ()): while (Have_posts ()): The_post ();?> <!– This is the list of articles listed when judging the conformity, you can use < PHP the_xxx (); > series to display relevant article information –> <?php endwhile; else:?> <!– This side is to show grab no data to run out of error messages –> <?php endif; Reset the query (this is to avoid later query data because of the above query and confusion) wp_reset_query ();?>


All of the following functions are based on the above frame, as long as the query_posts (); Enter the appropriate parameters to display the range of articles you want.


1, Category Parameters (article classification parameters)

<?php//Show only Articles with category number 4 (including subcategory articles) Query_posts (' cat=4′); Show only articles with category name Codex (including subcategory articles) query_posts (' Category_name=codex '); Displays articles within multiple categories (including sub-categories articles) query_posts (' cat=2,6,17,38′); In addition to articles numbered 3 (including sub-classified articles), other articles show query_posts (' cat=-3′); Displays the article query_posts (Array (' category__and ' = = = Array (2,6)) with the classification number 2 and 6 at the same time; Displays the article with category number 6 (but does not include the subcategory article) query_posts (Array (' category__in ' = = Array (6))); In addition to the articles numbered 2 and 6, subcategories and other classified articles show query_posts (Array (' category__not_in ' = = Array (2,6)));?>

2. Tag Parameters (tag parameter)

<?php//Show only the label with cooking article query_posts (' tag=cooking '); Show only articles with labels with bread or baking query_posts (' tag=bread,baking '); Show only articles tagged with bread and baking and recipe query_posts (' tag=bread+baking+recipe '); Only the labels with the articles numbered 37 and 47 are shown query_posts (Array (' tag__and ' = ' = ' = ' = ' = ' = ' = ' = ' = ' = ' = ' = ' = ' = ' = ' = ' = ' 37,47 ') In ' = = Array (37,47)); Show only articles with labels that are not numbered 37 or 47 query_posts (Array (' tag__not_in ' = = Array (37,47));?>

3. Author Parameters (author parameters)

<?php//Show only articles with author number 3 query_posts (' author=3′); Only articles with author number not 3 are displayed query_posts (' author=-3′); Only articles query_posts (' Author_name=harriet ') with author name Harriet are displayed;?>

4. Sticky Post Parameters (sticky article parameters)

<?php//Show only the pinned article array (' post__in ' =>get_option (' sticky_posts '))//Remove the top attributes of the article and display it in a normal article order (e.g., publication date) Caller_get _posts=1//Displays the article, but does not display the pinned article. Query_posts (Array ("Post__not_in" =>get_option ("sticky_posts")); Show Articles with category number 6, 3 articles per page, and clear the top properties of articles under this category, sorted by normal article order (such as publication date). Query_posts (' cat=6&posts_per_page=3&caller_get_posts=1′);?>

5, Post & page Parameters (articles & paging parameters)

<?php //Display article number 27 article   ' P '  => 27 //show article generation called About-my-life article   ' name '  =>   ' About-my-life '  //show pagination numbered 7 for pagination   ' page_id '  => 7 //show paging generation called about pagination   ' PageName '  =>  ' about '  //shows only 5 articles when the article is over 5 and displays the page-break link with the change-page code, which is set to 1 without a page change.   ' Posts_per_page '  => 5 //when set to 6 o'clock displays 6 articles, set to 1 to display all articles within the range.   ' showposts '  => 6 //show only 5 articles in article number 5,12,2,14,7   ' post__in '  => array ( 5,12,2,14,7)  //only show all other articles with article number not 5,12,2,14,7   ' post__not_in '  => array (6,2,8)  // The article that displays the article type is paginated, the default value is post  (article), the values you can use are attachment (media file page),  page (pagination),  post (article), or revision (revision).   ' post_type '  =>  ' page '  //Show article status for public articles, values that can be used are pending (in review),  draft (draft),  Future (scheduled),  private (private),  trash (garbage)  .   ' Post_status '  =>  ' publish '  //displays the 93rd page of the article in the range   ' post_parent '  => 93 ? >

6, Time Parameters (parameters)

<?php//Displays a list of articles published on December 20. Query_posts (' monthnum=12&day=20′); Displays a list of articles published this week. $week = Date (' W '); $year = Date (' Y '); Query_posts (' year= '. $year. ' &w= '. $week); Displays a list of articles published within the last 30 days. function Filter_where ($where = ") {$where. =" and Post_date > ' ". Date (' ym-d ', Strtotime (' -30 days ')). “‘”; return $where; } add_filter (' Posts_where ', ' filter_where '); Query_posts ($query _string);?>

7. Parameters (order parameter)

<?php//According to the author Orderby=author//According to the date Orderby=date//According to the title Orderby=title//According to the last editing time arrangement orderby=modified// Arranged in pagination order (only for pagination) Orderby=menu_order//(not known xd ...) orderby=parent//According to the article number Orderby=id//random arrangement Orderby=rand//According to the custom field value arrangement Orderby=meta_value//According to the preset arrangement Orderby=none//According to the number of echoes Orderby=comment_count?>

8, pagination Parameters (paging parameter)

<?php//The value is set to True when the display is not paginated, directly display all articles nopaging=true//display each page of the article shows 10 posts_per_page=10//pages, for example, when set to 6 will be skipped to the 6th page paged=6// The order in which ASC arranges the articles in chronological order, and if DESC is the reverse display of the article Order=asc?>

9. Combination application Example

<?php query_posts (' cat=3&year=2004′);?> Displays the article numbered 1 and 3 with two articles per page, in reverse order of headings. <?php query_posts (Array (' Category__and ' =>array (1,3), ' posts_per_page ' =>2, ' ORDER by ' =>title >DESC));?> is displayed only on the first page and is published in the month in which the classification number is 13. <?php if (Is_home ()) {query_posts ($query _string. ' &cat=13&monthnum= '. Date (' n ', current_time (' timestamp '))); }?> displays articles with a category number of 1 labeled apples. <?php query_posts (' cat=1&tag=apples ');?>


WordPress powerful invocation of the article function query posts usage

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.