当前位置:首页>百宝箱>正文

湛江网站建设常常用到的WP_Query的所有参数

发布时间:2018-10-28 20:45:41 作者:小超越君

不知道从何开始慢慢的爱上了wordpress,湛江网站建设大部分利用wordpress作为内核进行二次开发,无论客户的要求有多高或多简单,都能很轻易的完成,也正是因为如此,才对wordpress更深入的了解,人接触的东西多了,容易忘,小超越也一样,因为每天接触的东西不止是编程,还有许许多多的东西,也许,随着时间的悄然流逝,有一天,再想回头来对wordpress进行深度开发,就忘记了很多常用的函数。

老师曾经说过,好记性不如烂笔头,是的,小超越100%的相信,所以才有了这篇文章!当然,以下的内容并不是小超越整理的,前辈们已经整理出来了,我们还何必再浪费时间去整理呢?是吧!

WP_Query 是 WordPress 的核心,它支持的参数非常灵活,也非常多,官方的文档也略嫌啰嗦,整理把所有的参数都整理了一遍,以后要使用 WP_Query 只要看这份文档就够了:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
$args = array(

//////Author Parameters - Show posts associated with certain author.
    //http://codex.wordpress.org/Class_Reference/WP_Query#Author_Parameters
    'author' => '1,2,3,',                     //(int) - use author id [use minus (-) to exclude authors by ID ex. 'author' => '-1,-2,-3,']
    'author_name' => 'luetkemj',              //(string) - use 'user_nicename' (NOT name)
    'author__in' => array( 2, 6 ),            //(array) - use author id (available with Version 3.7).
    'author__not_in' => array( 2, 6 ),        //(array)' - use author id (available with Version 3.7).

//////Category Parameters - Show posts associated with certain categories.
    //http://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters
    'cat' => 5,//(int) - use category id.
    'category_name' => 'staff, news',          //(string) - Display posts that have these categories, using category slug.
    'category_name' => 'staff+news',           //(string) - Display posts that have "all" of these categories, using category slug.
    'category__and' => array( 2, 6 ),         //(array) - use category id.
    'category__in' => array( 2, 6 ),          //(array) - use category id.
    'category__not_in' => array( 2, 6 ),      //(array) - use category id.

//////Tag Parameters - Show posts associated with certain tags.
    //http://codex.wordpress.org/Class_Reference/WP_Query#Tag_Parameters
    'tag' => 'cooking',                       //(string) - use tag slug.
    'tag_id' => 5,                            //(int) - use tag id.
    'tag__and' => array( 2, 6),               //(array) - use tag ids.
    'tag__in' => array( 2, 6),                //(array) - use tag ids.
    'tag__not_in' => array( 2, 6),            //(array) - use tag ids.
    'tag_slug__and' => array( 'red', 'blue'), //(array) - use tag slugs.
    'tag_slug__in' => array( 'red', 'blue'),  //(array) - use tag slugs.

//////Taxonomy Parameters - Show posts associated with certain taxonomy.
    //http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters
    //Important Note: tax_query takes an array of tax query arguments arrays (it takes an array of arrays)
    //This construct allows you to query multiple taxonomies by using the relation parameter in the first (outer) array to describe the boolean relationship between the taxonomy queries.
    'tax_query' => array(                     //(array) - use taxonomy parameters (available with Version 3.1).
    'relation' => 'AND',                      //(string) - Possible values are 'AND' or 'OR' and is the equivalent of running a JOIN for each taxonomy
      array(
        'taxonomy' => 'color',                //(string) - Taxonomy.
        'field' => 'slug',                    //(string) - Select taxonomy term by ('id' or 'slug')
        'terms' => array( 'red', 'blue' ),    //(int/string/array) - Taxonomy term(s).
        'include_children' => true,           //(bool) - Whether or not to include children for hierarchical taxonomies. Defaults to true.
        'operator' => 'IN'                    //(string) - Operator to test. Possible values are 'IN', 'NOT IN', 'AND'.
      ),
      array(
        'taxonomy' => 'actor',
        'field' => 'id',
        'terms' => array( 103, 115, 206 ),
        'include_children' => false,
        'operator' => 'NOT IN'
      )
    ),
//////Post & Page Parameters - Display content based on post and page parameters.
    //http://codex.wordpress.org/Class_Reference/WP_Query#Post_.26_Page_Parameters
    'p' => 1,                               //(int) - use post id.
    'name' => 'hello-world',                //(string) - use post slug.
    'page_id' => 1,                         //(int) - use page id.
    'pagename' => 'sample-page',            //(string) - use page slug.
    'pagename' => 'contact_us/canada',      //(string) - Display child page using the slug of the parent and the child page, separated ba slash
    'post_parent' => 1,                     //(int) - use page id. Return just the child Pages. (Only works with heirachical post types.)
    'post_parent__in' => array(1,2,3)       //(array) - use post ids. Specify posts whose parent is in an array. NOTE: Introduced in 3.6
    'post_parent__not_in' => array(1,2,3),  //(array) - use post ids. Specify posts whose parent is not in an array.
    'post__in' => array(1,2,3),             //(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(1,2,3),         //(array) - use post ids. Specify post NOT to retrieve.
    //NOTE: you cannot combine 'post__in' and 'post__not_in' in the same query
//////Password Parameters - Show content based on post and page parameters. Remember that default post_type is only set to display posts but not pages.
    //http://codex.wordpress.org/Class_Reference/WP_Query#Password_Parameters
    'has_password' => true,                 //(bool) - available with Version 3.9
                                              //true for posts with passwords;
                                              //false for posts without passwords;
                                              //null for all posts with and without passwords
    'post_password' => 'multi-pass',          //(string) - show posts with a particular password (available with Version 3.9)
//////Type & Status Parameters - Show posts associated with certain type or status.
    //http://codex.wordpress.org/Class_Reference/WP_Query#Type_Parameters
    'post_type' => array(                   //(string / array) - use post types. Retrieves posts by Post Types, default value is 'post';
            'post',                         // - a post.
            'page',                         // - a page.
            'revision',                     // - a revision.
            'attachment',                   // - an attachment. The default WP_Query sets 'post_status'=>'published', but atchments default to 'post_status'=>'inherit' so you'll need to set the status to 'inherit' or 'any'.
            'my-post-type',                 // - Custom Post Types (e.g. movies)
            ),
    //NOTE: The 'any' keyword available to both post_type and post_status queries cannot be used within an array.
    'post_type' => 'any',                   // - retrieves any type except revisions and types with 'exclude_from_search' set to true.
//////Type & Status Parameters - Show posts associated with certain type or status.
    //http://codex.wordpress.org/Class_Reference/WP_Query#Status_Parameters
    'post_status' => array(                 //(string / array) - use post status. Retrieves posts by Post Status, default value i'publish'.
            '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 with Version 2.9).
            ),
    //NOTE: The 'any' keyword available to both post_type and post_status queries cannot be used within an array.
    'post_status' => 'any',                 // - retrieves any status except those from post types with 'exclude_from_search' set to true.

//////Pagination Parameters
    //http://codex.wordpress.org/Class_Reference/WP_Query#Pagination_Parameters
    'posts_per_page' => 10,                 //(int) - number of post to show per page (available with Version 2.1). Use 'posts_per_page' => -1 to show all posts.
                                            //Note: if the query is in a feed, wordpress overwrites this parameter with the stored 'posts_per_rss' option. Treimpose the limit, try using the 'post_limits' filter, or filter 'pre_option_posts_per_rss' and return -1
    'posts_per_archive_page' => 10,         //(int) - number of posts to show per page - on archive pages only. Over-rides showposts anposts_per_page on pages where is_archive() or is_search() would be true
    'nopaging' => false,                    //(bool) - show all posts or use pagination. Default value is 'false', use paging.
    'paged' => get_query_var('paged'),      //(int) - number of page. Show the posts that would normally show up just on page X when usinthe "Older Entries" link.
                                            //NOTE: Use get_query_var('page'); if you want your query to work in a Page template that you've set as your static front page. The query variable 'page' holds the pagenumber for a single paginated Post or Page that includes the lt;!--nextpage--gt; Quicktag in the post content.
    'nopaging' => false,                    // (boolean) - show all posts or use pagination. Default value is 'false', use paging.
    'posts_per_archive_page' => 10,         // (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' => 3,                          // (int) - number of post to displace or pass over.
                                            // Warning: Setting the offset parameter overrides/ignores the paged parameter and breaks pagination. for a workaround see: http://codex.wordpress.org/Making_Custom_Queries_using_Offset_and_Pagination
                                            // The 'offset' parameter is ignored when 'posts_per_page'=>-1 (show all posts) is used.
    'paged' => get_query_var('paged'),      //(int) - number of page. Show the posts that would normally show up just on page X when usinthe "Older Entries" link.
                                            //NOTE: This whole paging thing gets tricky. Some links to help you out:
                                              // http://codex.wordpress.org/Function_Reference/next_posts_link#Usage_when_querying_the_loop_with_WP_Query
                                              // http://codex.wordpress.org/Pagination#Troubleshooting_Broken_Pagination
    'page' => get_query_var('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.
                                            //NOTE: The query variable page holds the pagenumber for a single paginated Post or Page that includes the  nextpage Quicktag in the post content.

    'ignore_sticky_posts' => false,         // (boolean) - ignore sticky posts or not (available with Version 3.1, replaced caller_get_posts parameter). Default value is 0 - don't ignore sticky posts. Note: ignore/exclude sticky posts being included at the beginning of posts returned, but the sticky post will still be returned in the natural order of that list of posts returned.
//////Order & Orderby Parameters - Sort retrieved posts.
    //http://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters
    'order' => 'DESC',                      //(string) - Designates the ascending or descending order of the 'orderby' parameter. Default to 'DESC'.
                                              //Possible Values:
                                              //'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' => 'date',                    //(string) - Sort retrieved posts by parameter. Defaults to 'date'. One or more options can be passed. EX: 'orderby' => 'menu_order title'
                                              //Possible Values:
                                              //'none' - No order (available with Version 2.8).
                                              //'ID' - Order by post id. Note the captialization.
                                              //'author' - Order by author.
                                              //'title' - Order by title.
                                              //'name' - Order by post name (post slug).
                                              //'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 with Version 2.9).
                                              //'menu_order' - Order by Page Order. Used most often for Pages (Order field in the EdiPage Attributes box) and for Attachments (the integer fields in the Insert / Upload MediGallery dialog), but could be used for any post type with distinct 'menu_order' values (theall default to 0).
                                              //'meta_value' - Note that a 'meta_key=keyname' must also be present in the query. Note alsthat the sorting will be alphabetical which is fine for strings (i.e. words), but can bunexpected for numbers (e.g. 1, 3, 34, 4, 56, 6, etc, rather than 1, 3, 4, 6, 34, 56 as yomight naturally expect).
                                              //'meta_value_num' - Order by numeric meta value (available with Version 2.8). Also notthat a 'meta_key=keyname' must also be present in the query. This value allows for numericasorting as noted above in 'meta_value'.
                                              //'title menu_order' - Order by both menu_order AND title at the same time. For more info see: http://wordpress.stackexchange.com/questions/2969/order-by-menu-order-and-title
                                              //'post__in' - Preserve post ID order given in the post__in array (available with Version 3.5).

//////Date Parameters - Show posts associated with a certain time and date period.
    //http://codex.wordpress.org/Class_Reference/WP_Query#Date_Parameters
    'year' => 2014,                         //(int) - 4 digit year (e.g. 2011).
    'monthnum' => 4,                        //(int) - Month number (from 1 to 12).
    'w' =>  25,                             //(int) - Week of the year (from 0 to 53). Uses the MySQL WEEK command. The mode is dependenon the "start_of_week" option.
    'day' => 17,                            //(int) - Day of the month (from 1 to 31).
    'hour' => 13,                           //(int) - Hour (from 0 to 23).
    'minute' => 19,                         //(int) - Minute (from 0 to 60).
    'second' => 30,                         //(int) - Second (0 to 60).
    'm' => 201404,                          //(int) - YearMonth (For e.g.: 201307).
    'date_query' => array(                  //(array) - Date parameters (available with Version 3.7).
                                              //these are super powerful. check out the codex for more comprehensive code examples http://codex.wordpress.org/Class_Reference/WP_Query#Date_Parameters
      array(
        'year' => 2014,                     //(int) - 4 digit year (e.g. 2011).
        'month' => 4                        //(int) - Month number (from 1 to 12).
        'week' => 31                        //(int) - Week of the year (from 0 to 53).
        'day' => 5                          //(int) - Day of the month (from 1 to 31).
        'hour' => 2                         //(int) - Hour (from 0 to 23).
        'minute' => 3                       //(int) - Minute (from 0 to 59).
        'second' => 36                      //(int) - Second (0 to 59).
        'after'     => 'January 1st, 2013', //(string/array) - Date to retrieve posts after. Accepts strtotime()-compatible string, or array of 'year', 'month', 'day'
        'before'    => array(               //(string/array) - Date to retrieve posts after. Accepts strtotime()-compatible string, or array of 'year', 'month', 'day'
          'year'  => 2013,                  //(string) Accepts any four-digit year. Default is empty.
          'month' => 2,                     //(string) The month of the year. Accepts numbers 1-12. Default: 12.
          'day'   => 28,                    //(string) The day of the month. Accepts numbers 1-31. Default: last day of month.
        ),
        'inclusive' => true,                //(boolean) - For after/before, whether exact value should be matched or not'.
        'compare' =>  '=',                  //(string) - Possible values are '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN', 'EXISTS' (only in WP >= 3.5), and 'NOT EXISTS' (also only in WP >= 3.5). Default value is '='
        'column' => 'post_date',            //(string) - Column to query against. Default: 'post_date'.
        'relation' => 'AND',                //(string) - OR or AND, how the sub-arrays should be compared. Default: AND.
      ),
    ),
//////Custom Field Parameters - Show posts associated with a certain custom field.
    //http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters
    'meta_key' => 'key',                    //(string) - Custom field key.
    'meta_value' => 'value',                //(string) - Custom field value.
    'meta_value_num' => 10,                 //(number) - Custom field value.
    'meta_compare' => '=',                  //(string) - Operator to test the 'meta_value'. Possible values are '!=', '>', '>=', '<', or ='. Default value is '='.
    'meta_query' => array(                  //(array) - Custom field parameters (available with Version 3.1).
       'relation' => 'AND',                 //(string) - Possible values are 'AND', 'OR'. The logical relationship between each inner meta_query array when there is more than one. Do not use with a single inner meta_query array.
       array(
         'key' => 'color',                  //(string) - Custom field key.
         'value' => 'blue'                  //(string/array) - Custom field value (Note: Array support is limited to a compare value of 'IN', 'NOT IN', 'BETWEEN', or 'NOT BETWEEN') Using WP < 3.9? Check out this page for details: http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters
         'type' => 'CHAR',                  //(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 YYYYMMDD and tested with this format.
                                            //NOTE: The 'type' DATE works with the 'compare' value BETWEEN only if the date is stored at the format YYYYMMDD and tested with this format.
         'compare' => '='                   //(string) - Operator to test. Possible values are '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN', 'EXISTS' (only in WP >= 3.5), and 'NOT EXISTS' (also only in WP >= 3.5). Default value is '='.
       ),
       array(
         'key' => 'price',
         'value' => array( 1,200 ),
         'compare' => 'NOT LIKE'
       )
    ),
//////Permission Parameters - Display published posts, as well as private posts, if the user has the appropriate capability:
    //http://codex.wordpress.org/Class_Reference/WP_Query#Permission_Parameters
    'perm' => 'readable'                    //(string) Possible values are 'readable', 'editable'
//////Caching Parameters
    //http://codex.wordpress.org/Class_Reference/WP_Query#Caching_Parameters
    //NOTE Caching is a good thing. Setting these to false is generally not advised.
    'cache_results' => true,                //(bool) Default is true - Post information cache.
    'update_post_term_cache' => true,       //(bool) Default is true - Post meta information cache.
    'update_post_meta_cache' => true,       //(bool) Default is true - Post term information cache.

    'no_found_rows' => false,               //(bool) Default is false. WordPress uses SQL_CALC_FOUND_ROWS in most queries in order to implement pagination. Even when you don’t need pagination at all. By Setting this parameter to true you are telling wordPress not to count the total rows and reducing load on the DB. Pagination will NOT WORK when this parameter is set to true. For more information see: http://flavio.tordini.org/speed-up-wordpress-get_posts-and-query_posts-functions

//////Search Parameter
    //http://codex.wordpress.org/Class_Reference/WP_Query#Search_Parameter
    's' => $s,                              //(string) - Passes along the query string variable from a search. For example usage see: http://www.wprecipes.com/how-to-display-the-number-of-results-in-wordpress-search
    'exact' => true,                        //(bool) - flag to make it only match whole titles/posts - Default value is false. For more information see: https://gist.github.com/2023628#gistcomment-285118
    'sentence' => true,                     //(bool) - flag to make it do a phrase search - Default value is false. For more information see: https://gist.github.com/2023628#gistcomment-285118
//////Post Field Parameters
    //For more info see: http://codex.wordpress.org/Class_Reference/WP_Query#Return_Fields_Parameter
    //also https://gist.github.com/luetkemj/2023628/#comment-1003542
    'fields' => 'ids'                       //(string) - Which fields to return. All fields are returned by default.
                                              //Possible values:
                                              //'ids'        - Return an array of post IDs.
                                              //'id=>parent' - Return an associative array [ parent => ID, … ].
                                              //Passing anything else will return all fields (default) - an array of post objects.
//////Filters
    //For more information on available Filters see: http://codex.wordpress.org/Class_Reference/WP_Query#Filters
);

感谢我爱水煮鱼大神的整理以上文档,不过是英文注释,看不懂得同学可以看下面的!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
'1,2,3,' //(整数) - 作者ID [使用减号 (-) 排除某个作者 ID, 如: 'author' => '-1,-2,-3,']'author_name' => 'luetkemj', //(字符串) - 使用 'user_nicename' 用户昵称,(不是名称)
//////分类参数 - 显示某个分类里面的文章
'cat' => 5,//(整数) - 分类id
'category_name' => 'staff', 'news', //(字符串) - 分类别名(不是名称)
'category__and' => array( 2, 6 ), //(数组) - 分类id
'category__in' => array( 2, 6 ), //(数组) - 分类id
'category__not_in' => array( 2, 6 ), //(数组) - 分类id

/////标签参数 - 显示含有某些标签的文章
'tag' => 'cooking', //(字符串) - 标签别名
'tag_id' => 5, //(整数) -标签id
'tag__and' => array( 2, 6), //(数组) - 标签id
'tag__in' => array( 2, 6), //(数组) - 标签id
'tag__not_in' => array( 2, 6), //(数组) - 标签id
'tag_slug__and' => array( 'red', 'blue'), //(数组) - 标签别名
'tag_slug__in' => array( 'red', 'blue'), //(数组) - 标签别名

//////自定义分类法参数 - 显示某些自定义分类法里面的文章
//重要提示: tax_query 使用多维数组
//这种查询结构允许我们查询多个自定义分类法
'tax_query' => array( //(数组) - 使用自定义分类法查询参数 (3.1及以后版本可用).
'relation' => 'AND', //(字符串) - 可用的值有 'AND' 或 'OR' 和 SQL 的 JOIN 作用是相同的
array(
'taxonomy' => 'color', //(字符串) - 自定义分类法
'field' => 'slug', //(字符串) - 使用别名还是分类作为查询条件 ('id' 或 'slug')
'terms' => array( 'red', 'blue' ), //(整数/字符串/数组) - 自定义分类法分类条目
'include_children' => true, //(布尔值) - 是否包含自分类,默认为真
'operator' => 'IN' //(字符串) - 测试条件,可用值为 'IN', 'NOT IN', 'AND'.
),
array(
'taxonomy' => 'actor',
'field' => 'id',
'terms' => array( 103, 115, 206 ),
'include_children' => false,
'operator' => 'NOT IN'
)
),

//////文章 & 页面参数- 基于文章或页面参数显示文章
'p' => 1, //(整数) - 文章id
'name' => 'hello-world', //(字符串) - 文章别名
'page_id' => 1, //(整数) - 页面id
'pagename' => 'sample-page', //(字符串) - 页面别名
'pagename' => 'contact_us/canada', //(字符串) - 用斜杠‘/’分割的父页面别名/子页面别名来显示子页面
'post_parent' => 1, //(整数) - 页面id,只返回子页面,只对有子页面的页面有效
'post__in' => array(1,2,3), //(数组) - 需要显示的文章的id
'post__not_in' => array(1,2,3), //(数组) - 需要排除的文章的id
//注意:不能在同一个查询里同时使用 'post__in' 和 'post__not_in'

//////文章类型 & 状态参数 - 显示某些文章类型里面的文章
'post_type' => array( //(字符串/ 数组) - 文章类型,根据文章类型获取文章,默认为'post'
'post', // - 文章
'page', // - 页面
'revision', // - 文章版本
'attachment', // - 附件,默认 WP_Query 设置了发布状态为 'post_status'=>'published', 但是附件默认为 'post_status'=>'inherit',所以你需要设置状态为 'inherit' 或'any'.
'my-post-type', // - 自定义文章类型 (例如:movies)
),
'post_status' => array( //(字符串 / 数组) - 使用文章状态,根据文章状态获取文章,默认为 'publish'
'publish', // - 已发布的文章或页面
'pending', // -等待复审的文章
'draft', // - 处于草稿状态的文章
'auto-draft', // - 自动保存为草稿的文章
'future', // - 定时发布的文章
'private', // - 未登录用户不能查看的私有文章
'inherit', // - 版本. 具体参考 get_children.
'trash' // - 回收站中的文章 (2.9和以后的版本可用).
),

//注意:The 'any' 关键字可以用在 post_type 和 post_status 查询,但是不能在数组中使用
'post_type' => 'any', // - 获取所有文章类型里面的文章,除了版本和文章类型参数'exclude_from_search'设置为true的文章类型
'post_status' => 'any', // - 获取处于所有文章状态的文章,除了版本和文章类型参数'exclude_from_search'设置为true的文章类型

//////分页参数
'posts_per_page' => 10, //(整数) - 每页显示的文章数量 (2.1和以后的版本可用), 使用'posts_per_page'=-1 显示所有文章,如果查询处于订阅源中,WordPress用 'posts_per_rss' 选项覆盖了这里的设置,需要使用这个限制,尝试使用 'post_limits' 过滤器,或使用 'pre_option_posts_per_rss'过滤器返回 -1
'posts_per_archive_page' => 10, //(整数) - n每页显示的文章数量 - 只在存档页面使用,在存档页面和搜索结果页面覆盖了 showposts 和 posts_per_page 参数
'nopaging' => false, //(布尔值) - 在一页显示所有文章或使用分页,默认值为 'false', 使用分页
'paged' => get_query_var('paged'), //(整数) - 页数,分页时显示第几页
//注意:使用 get_query_var('page'); 如果查询在设置为首页的页面模版中工作,查询参数 'page' 拥有文章分页或内容中使用快捷代码的分页。

//////偏移参数
'offset' => 3, //(int) - 跳过的文章数量

//////排序 & 排序方式参数 - 对获取的文章进行排序
'order' => 'DESC', //(字符串) - 设置 'order_by' 参数升序或降序排列. 默认为'DESC'.
//Possible Values:
//'ASC' - 升序排列,从小到大 (1, 2, 3; a, b, c).
//'DESC' - 降序排列,从大到小 (3, 2, 1; c, b, a).
'orderby' => 'date', //(字符串) - 排序依据. 默认为 'date'.
//可用的参数有://
//'none' - 不排序 (2.8和以后的版本可用)
//'ID' - 根据ID排序,注意ID是大写的
//'author' - 根据作者排序
//'title' - 根据标题排序
//'date' - 根据发表时间排序
//'modified' - 根据最后修改时间排序
//'parent' - 根据父页面排序
//'rand' - 随机排序
//'comment_count' - 根据评论数量排序 (2.9和以后的版本可用).
//'menu_order' - 根据页面序号排序. 通常在页面中使用 (编辑页面时有一个页面序号的字段) 和附件 ( 插入 / 上传媒体相册对话框中的数字), 但是不能对文章类型 'menu_order' 使用数字值 (默认都为 0).
//'meta_value' - 注意'meta_key=keyname' 必须也出现在查询中. 注意排序是按照字母表顺序进行的。(如:words),但是数字排序可能会有问题 (如:1, 3, 34, 4, 56, 6, etc, 而不是你希望的:1, 3, 4, 6, 34, 56)。
//'meta_value_num' - 根据数字meta值排序 (2.8和以后的版本中可用). 同时需要注意'meta_key=keyname' 也要在查询中声明。这个值和上面说明的 'meta_value' 一样,只不过值允许使用数字排序。
//'title menu_order' - 同时使用 menu_order 和 title 排序 更多信息请参考:http://wordpress.stackexchange.com/questions/2969/order-by-menu-order-and-title
//'post__in' - 使用 post__in 数组中制定的 ID 顺序 (3.5以后的版本中可用).

//////置顶文章参数 - 显示或忽略置顶文章
'ignore_sticky_posts' => false, //(布尔值) - 是否忽略置顶文章,默认为假不忽略. 在返回文章的开头忽略/排除置顶文章,但是置顶文章还是会在自然查询中列出。
//注意:关于置顶文章的更多信息,请参考:http://codex.wordpress.org/Class_Reference/WP_Query#Sticky_Post_Parameters

//////时间参数 - 显示某个时间段内的文章
'year' => 2012, //(int) - 4 个数字的年份 (如:2011)
'monthnum' => 3, //(int) - 月份数字 (从 1 到 12)
'w' => 25, //(int) - 一年中的第几周 (从 0 到 53), 使用 MySQL WEEK 命令,此模式和"start_of_week" 选项相关
'day' => 17, //(int) - 月中的天数 (从 1 到 31)
'hour' => 13, //(int) - 小时 (从 0 到 23).
'minute' => 19, //(int) - 分钟 (从 0 到 60).
'second' => 30, //(int) - 秒 (从 0 到 60).

//////自定义字段参数 - 显示拥有某个自定义字段的文章
'meta_key' => 'key', //(字符串) - 自定义字段的键
'meta_value' => 'value', //(字符串) - 自定义字段的值
'meta_value_num' => 10, //(数字) - 自定义字段的值
'meta_compare' => '=', //(字符串) - 测试'meta_value'的操作。可用的值有'!=', '>', '>=', '<', or ='. 默认为 '='. 'meta_query' => array( //(数组) - 自定义字段参数 (3.1和以后的版本可用).
array(
'key' => 'color', //(字符串) - 自定义字段的键
'value' => 'blue' //(字符串/数组) - 自定义字段的值 (注意:数组的支持仅限于一个比较值: 'IN', 'NOT IN', 'BETWEEN', or 'NOT BETWEEN')
'type' => 'CHAR', //(字符串) -自定义字段类型,可用的值有:'NUMERIC', 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED',默认为 'CHAR'
'compare' => '=' //(字符串) - 测试的操作,可用的值有: '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN'. 默认为:'=' ), array( 'key' => 'price',
'value' => array( 1,200 ),
'compare' => 'NOT LIKE'
)

/////权限参数 - 显示已发布文章,如果用户有合适的权限,同样现实私有文章:
'perm' => 'readable' //(字符串) 可用的值有:'readable', 'editable' (可能还有其他可用的值我没有测试)

//////与缓存相关的参数
'no_found_rows' => false, //(布尔值) 默认为假,为了分页,WordPress 在大多数查询中使用 SQL_CALC_FOUND_ROWS 查询, 即使你不需要分页,通过设置这个参数为真,我们告诉了了WordPress不要查询数据总行数,从而降低数据库负载,如果设置了这个参数为真,分页将不工作,更多信息请参考:http://flavio.tordini.org/speed-up-wordpress-get_posts-and-query_posts-functions
'cache_results' => true, //(布尔值) 默认为真
'update_post_term_cache' => true, //(布尔值) 默认为真
'update_post_meta_cache' => true, //(布尔值) 默认为真
//注意:缓存是个好东西,通常不建议设为假,更多信息请参考:http://codex.wordpresorg/Class_Reference/WP_Query#Permission_Parameters

//////搜索参数
's' => $s, //(字符串) - 传递搜索变量到搜索功能,更多信息请参考: http://www.wprecipes.com/how-to-display-the-number-of-results-in-wordpress-search
'exact' => true //(布尔值) - 只匹配完整的titles/posts的信号 - 默认值为假,更多信息请参考:https://gist.github.com/2023628#gistcomment-285118
'sentence' => true //(布尔值) - 进行短语搜索的信号-默认值为假,更多信息请参考:https://gist.github.com/2023628#gistcomment-285118

//////文章字段参数
//关于文章字段参数信息,请参考http://codex.wordpress.org/Class_Reference/WP_Query#Post_Field_Parameters

//////过滤器
//关于过滤器的更多信息,请参考:http://codex.wordpress.org/Class_Reference/WP_Query#Filters

);

$the_query = new WP_Query( $args );

// 循环开始
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
// 输出内容
endwhile;
endif;

// 重置文章数据
wp_reset_postdata();

?>

文章内容欢迎转载,请保留本文链接:https://www.zhe94.com/402.html

专注高端湛江网站建设及湛江SEO优化服务 立即咨询