一、语法

query_posts( array|string $query )

通过一些参数作为查询条件,在文章循环中查询满足条件的文章。它被用在循环中。

 

二、该函数的用法

用在文章循环函数中

<?php if (have_posts) : ?>
<?php query_posts( $query ); ?>
<?php while ( have_posts() ) : the_post(); ?>
...
<?php endwhile; ?>
<?php endif; wp_reset_query(); ?>

三、该函数的使用方法演示

<?php query_posts('cat=3&order=ASC&orderby=id&posts_per_page=5'); ?> 查找分类是3、按升序排序,用id值来排序,posts_per_page每页展示5篇文章。

四、该函数的参数们

参数$query可以是字符串,也可以是数组,字符串或数据都包含下面这些参数

作者参数(Author Parameters )

  • author (int) – use author id.
  • author_name (string) – use ‘user_nicename‘ – NOT name.
  • author__in (array) – use author id (available since version 3.7).
  • author__not_in (array) – use author id (available since version 3.7).

文章分类参数(Category Parameters )

  • cat (int) – use category id.
  • category_name (string) – use category slug.
  • category__and (array) – use category id.
  • category__in (array) – use category id.
  • category__not_in (array) – use category id.

文章标签参数(Tag Parameters)

Show posts associated with certain tags.

  • tag (string) – use tag slug.
  • tag_id (int) – use tag id.
  • tag__and (array) – use tag ids.
  • tag__in (array) – use tag ids.
  • tag__not_in (array) – use tag ids.
  • tag_slug__and (array) – use tag slugs.
  • tag_slug__in (array) – use tag slugs.

自定义分类或标签参数(Taxonomy Parameters)

  • {tax} (string) – use taxonomy slug. (Deprecated since version 3.1 in favor of ‘tax_query‘).
  • tax_query (array) – use taxonomy parameters (available since version 3.1).
    • relation (string) – The logical relationship between each inner taxonomy array when there is more than one. Possible values are ‘AND’, ‘OR’. Do not use with a single inner taxonomy array.
      • taxonomy (string) – Taxonomy.
      • field (string) – Select taxonomy term by. Possible values are ‘term_id’, ‘name’, ‘slug’ or ‘term_taxonomy_id’. Default value is ‘term_id’.
      • terms (int/string/array) – Taxonomy term(s).
      • include_children (boolean) – Whether or not to include children for hierarchical taxonomies. Defaults to true.
      • operator (string) – Operator to test. Possible values are ‘IN’, ‘NOT IN’, ‘AND’, ‘EXISTS’ and ‘NOT EXISTS’. Default value is ‘IN’.

搜索参数(Search Parameters )

Show posts based on a keyword search.

  • s (string) – Search keyword.

文章&页面参数(Post & Page Parameters)

  • p (int) – use post id.
  • name (string) – use post slug.
  • page_id (int) – use page id.
  • pagename (string) – use page slug.
  • post_parent (int) – use page id to return only child pages. Set to 0 to return only top-level entries.
  • post_parent__in (array) – use post ids. Specify posts whose parent is in an array. (available since version 3.6)
  • post_parent__not_in (array) – use post ids. Specify posts whose parent is not in an array. (available since version 3.6)
  • post__in (array) – use post ids. Specify posts to retrieve. ATTENTION If you use sticky posts, they will be included (prepended!) in the posts you retrieve whether you want it or not. To suppress this behaviour use ignore_sticky_posts.
  • post__not_in (array) – use post ids. Specify post NOT to retrieve.
  • post_name__in (array) – use post slugs. Specify posts to retrieve. (Will be available in version 4.4)

密码参数(Password Parameters)

Show content based on post and page parameters. Remember that default post_type is only set to display posts but not pages.

  • has_password (bool) – true for posts with passwords ; false for posts without passwords ; null for all posts with and without passwords (available since version 3.9).
  • post_password (string) – show posts with a particular password (available since version 3.9)

文章类型参数(Post Type Parameters )

  • post_type (string / array) – use post types. Retrieves posts by post types, default value is ‘post‘. If ‘tax_query‘ is set for a query, the default value becomes ‘any‘;
    • post‘ – a post.
    • page‘ – a page.
    • revision‘ – a revision.
    • attachment‘ – an attachment. Whilst the default WP_Query post_status is ‘publish’, attachments have a default post_status of ‘inherit’. This means no attachments will be returned unless you also explicitly set post_status to ‘inherit’ or ‘any’.
    • nav_menu_item‘ – a navigation menu item
    • any‘ – retrieves any type except revisions and types with ‘exclude_from_search’ set to true.

文章状态参数(Status Parameters)

Show posts associated with certain post status.

  • post_status (string / array) – use post status. Retrieves posts by post status. Default value is ‘publish‘, but if the user is logged in, ‘private‘ is added. Public custom post statuses are also included by default. And if the query is run in an admin context (administration area or AJAX call), protected statuses are added too. By default protected statuses are ‘future‘, ‘draft‘ and ‘pending‘.
    • publish‘ – 已发布a published post or page.
    • pending‘ – 待审post is pending review.
    • draft‘ – 草稿a post in draft status.
    • auto-draft‘ – 自动草稿a newly created post, with no content.
    • future‘ – 定时a post to publish in the future.
    • private‘ – 私有not visible to users who are not logged in.
    • inherit‘ – 继承a revision. see get_children().
    • trash‘ – 已删除post is in trashbin (available since version 2.9).
    • any‘ – 任何所有retrieves any status except those from post statuses with ‘exclude_from_search’ set to true (i.e. trash and auto-draft).

文章分页参数(Pagination Parameters )

  • nopaging (boolean) – show all posts or use pagination. Default value is ‘false’, use paging.
  • posts_per_page (int) – number of post to show per page (available since version 2.1, replaced showposts parameter). Use 'posts_per_page'=>-1 to show all posts (the 'offset' parameter is ignored with a -1 value). Set the ‘paged’ parameter if pagination is off after using this parameter. Note: if the query is in a feed, wordpress overwrites this parameter with the stored ‘posts_per_rss’ option. To reimpose the limit, try using the ‘post_limits’ filter, or filter ‘pre_option_posts_per_rss’ and return -1
  • posts_per_archive_page (int) – number of posts to show per page – on archive pages only. Over-rides posts_per_page and showposts on pages where is_archive() or is_search() would be true.
  • offset (int) – number of post to displace or pass over. Warning: Setting the offset parameter overrides/ignores the paged parameter and breaks pagination. The 'offset'parameter is ignored when 'posts_per_page'=>-1 (show all posts) is used.
  • paged (int) – number of page. Show the posts that would normally show up just on page X when using the “Older Entries” link.
  • page (int) – number of page for a static front page. Show the posts that would normally show up just on page X of a Static Front Page.
  • ignore_sticky_posts (boolean) – ignore post stickiness (available since version 3.1, replaced caller_get_posts parameter). false (default): move sticky posts to the start of the set. true: do not move sticky posts to the start of the set.

文章排序参数(Order & Orderby Parameters)

Sort retrieved posts.

  • order (string | array) – Designates the ascending or descending order of the ‘orderby‘ parameter. Defaults to ‘DESC’. An array can be used for multiple order/orderby sets.
    • ASC‘ – ascending order from lowest to highest values (1, 2, 3; a, b, c).
    • DESC‘ – descending order from highest to lowest values (3, 2, 1; c, b, a).
  • orderby (string | array) – Sort retrieved posts by parameter. Defaults to ‘date (post_date)’. One or more options can be passed.
    • none‘ – No order (available since version 2.8).
    • ID‘ – Order by post id. Note the capitalization.
    • author‘ – Order by author.
    • title‘ – Order by title.
    • name‘ – Order by post name (post slug).
    • type‘ – Order by post type (available since version 4.0).
    • date‘ – Order by date.
    • modified‘ – Order by last modified date.
    • parent‘ – Order by post/page parent id.
    • rand‘ – Random order.
    • comment_count‘ – Order by number of comments (available since version 2.9).
    • menu_order‘ – Order by Page Order. Used most often for pages (Order field in the Edit Page Attributes box) and for attachments (the integer fields in the Insert / Upload Media Gallery dialog), but could be used for any post type with distinct ‘menu_order‘ values (they all default to 0).
    • meta_value‘ – Note that a ‘meta_key=keyname‘ must also be present in the query. Note also that the sorting will be alphabetical which is fine for strings (i.e. words), but can be unexpected for numbers (e.g. 1, 3, 34, 4, 56, 6, etc, rather than 1, 3, 4, 6, 34, 56 as you might naturally expect). Use ‘meta_value_num‘ instead for numeric values. You may also specify ‘meta_type‘ if you want to cast the meta value as a specific type. Possible values are ‘NUMERIC’, ‘BINARY’, ‘CHAR’, ‘DATE’, ‘DATETIME’, ‘DECIMAL’, ‘SIGNED’, ‘TIME’, ‘UNSIGNED’, same as in ‘$meta_query‘.
    • meta_value_num‘ – Order by numeric meta value (available since version 2.8). Also note that a ‘meta_key=keyname‘ must also be present in the query. This value allows for numerical sorting as noted above in ‘meta_value‘.
    • post__in‘ – Preserve post ID order given in the post__in array (available since version 3.5).

日期参数(Date Parameters )

Show posts associated with a certain time and date period.

  • year (int) – 4 digit year (e.g. 2011).
  • monthnum (int) – Month number (from 1 to 12).
  • w (int) – Week of the year (from 0 to 53). Uses MySQL WEEK command. The mode is dependent on the “start_of_week” option.
  • day (int) – Day of the month (from 1 to 31).
  • hour (int) – Hour (from 0 to 23).
  • minute (int) – Minute (from 0 to 60).
  • second (int) – Second (0 to 60).
  • m (int) – YearMonth (For e.g.: 201307).
  • date_query (array) – Date parameters (available since version 3.7).
    • year (int) – 4 digit year (e.g. 2011).
    • month (int) – Month number (from 1 to 12).
    • week (int) – Week of the year (from 0 to 53).
    • day (int) – Day of the month (from 1 to 31).
    • hour (int) – Hour (from 0 to 23).
    • minute (int) – Minute (from 0 to 59).
    • second (int) – Second (0 to 59).
    • after (string/array) – Date to retrieve posts after. Accepts strtotime()-compatible string, or array of ‘year’, ‘month’, ‘day’ values:
      • year (string) Accepts any four-digit year. Default is empty.
      • month (string) The month of the year. Accepts numbers 1-12. Default: 12.
      • day (string) The day of the month. Accepts numbers 1-31. Default: last day of month.
    • before (string/array) – Date to retrieve posts before. Accepts strtotime()-compatible string, or array of ‘year’, ‘month’, ‘day’ values:
      • year (string) Accepts any four-digit year. Default is empty.
      • month (string) The month of the year. Accepts numbers 1-12. Default: 1.
      • day (string) The day of the month. Accepts numbers 1-31. Default: 1.
    • inclusive (boolean) – For after/before, whether exact value should be matched or not’.
    • compare (string) – See WP_Date_Query::get_compare().
    • column (string) – Posts column to query against. Default: ‘post_date’.
    • relation (string) – OR or AND, how the sub-arrays should be compared. Default: AND.

自定义字段(文章元数据)参数(Custom Field (post meta) Parameters )

Show posts associated with a certain custom field.

This part of the query is parsed by WP_Meta_Query, so check the docs for it as well in case this list of arguments isn’t up to date.

  • meta_key (string) – Custom field key.
  • meta_value (string) – Custom field value.
  • meta_value_num (number) – Custom field value.
  • meta_compare (string) – Operator to test the ‘meta_value‘. Possible values are ‘=’, ‘!=’, ‘>’, ‘>=’, ‘<‘, ‘<=’, ‘LIKE’, ‘NOT LIKE’, ‘IN’, ‘NOT IN’, ‘BETWEEN’, ‘NOT BETWEEN’, ‘NOT EXISTS’, ‘REGEXP’, ‘NOT REGEXP’ or ‘RLIKE’. Default value is ‘=’.
  • meta_query (array) – Custom field parameters (available since version 3.1).
    • relation (string) – The logical relationship between each inner meta_query array when there is more than one. Possible values are ‘AND’, ‘OR’. Do not use with a single inner meta_query array.

meta_query also contains one or more arrays with the following keys:

  • key (string) – Custom field key.
  • value (string|array) – Custom field value. It can be an array only when compare is 'IN''NOT IN''BETWEEN', or 'NOT BETWEEN'. You don’t have to specify a value when using the 'EXISTS' or 'NOT EXISTS' comparisons in WordPress 3.9 and up.
    (Note: Due to bug #23268value is required for NOT EXISTS comparisons to work correctly prior to 3.9. You must supply some string for the value parameter. An empty string or NULL will NOT work. However, any other string will do the trick and will NOT show up in your SQL when using NOT EXISTS. Need inspiration? How about 'bug #23268'.)
  • compare (string) – Operator to test. Possible values are ‘=’, ‘!=’, ‘>’, ‘>=’, ‘<‘, ‘<=’, ‘LIKE’, ‘NOT LIKE’, ‘IN’, ‘NOT IN’, ‘BETWEEN’, ‘NOT BETWEEN’, ‘EXISTS’ and ‘NOT EXISTS’. Default value is ‘=’.
  • type (string) – Custom field type. Possible values are ‘NUMERIC’, ‘BINARY’, ‘CHAR’, ‘DATE’, ‘DATETIME’, ‘DECIMAL’, ‘SIGNED’, ‘TIME’, ‘UNSIGNED’. Default value is ‘CHAR’.

The ‘type’ DATE works with the ‘compare’ value BETWEEN only if the date is stored at the format YYYY-MM-DD and tested with this format.

Important Note: meta_query takes an array of meta query arguments arrays (it takes an array of arrays) – you can see this in the examples below.
This construct allows you to query multiple metadatas by using the relation parameter in the first (outer) array to describe the boolean relationship between the meta queries. Accepted arguments are ‘AND’, ‘OR’. The default is ‘AND’.

文章权限参数(Permission Parameters )

perm (string) – User permission.

Show posts if user has the appropriate capability:

缓存参数(Caching Parameters)

Stop the data retrieved from being added to the cache.

cache_results (boolean) – Post information cache.
update_post_meta_cache (boolean) – Post meta information cache.
update_post_term_cache (boolean) – Post term information cache.

返回字段参数(Return Fields Parameter)

Set return values.设置该函数返回值。

fields (string) – Which fields to return. There are several options:

'all' – Return all fields (default).
'ids' – Return an array of post IDs.
'id=>parent' – Return an array of stdClass objects with ID and post_parent properties.

博主联系方式:

  • 微信:34419369
  • QQ: 34419369
  • 公众号:前方录
  • 有什么不懂的地方欢迎联系我,帮到你是我会很开心

Leave a Reply

邮箱地址不会被公开。 必填项已用*标注