Функция WP_Query весь список аргументов | Создание сайтов Киев, продвижение сайтов | Студия "Движок"
|
info@dvigok.com.ua
Функция WP_Query весь список аргументов

Функция WP_Query весь список аргументов

|20.06.2021

Функция WP_Query один из наиболее часто используемых классов в работе WordPress-разработчика.

Данный пост является заметкой-сниппетом, в нем перечислены все аргументы которые могут передаваться в качестве параметра для выборки постов WordPress.

<span class="hljs-meta"><?php</span>
<span class="hljs-comment">/**
* WordPress Query Comprehensive Reference
* Compiled by luetkemj - luetkemj.com
*
* CODEX: http://codex.wordpress.org/Class_Reference/WP_Query#Parameters
* Source: https://core.trac.wordpress.org/browser/tags/3.9/src/wp-includes/query.php
*/</span>
$args = <span class="hljs-keyword">array</span>(
<span class="hljs-comment">// Author Parameters - Show posts associated with certain author.</span>
<span class="hljs-comment">// http://codex.wordpress.org/Class_Reference/WP_Query#Author_Parameters</span>
    <span class="hljs-string">'author'</span> => <span class="hljs-string">'1,2,3,'</span>                      <span class="hljs-comment">// (int) - use author id [use minus (-) to exclude authors by ID ex. 'author' => '-1,-2,-3,']</span>
    <span class="hljs-string">'author_name'</span> => <span class="hljs-string">'luetkemj'</span>,              <span class="hljs-comment">// (string) - use 'user_nicename' (NOT name)</span>
    <span class="hljs-string">'author__in'</span> => <span class="hljs-keyword">array</span>( <span class="hljs-number">2</span>, <span class="hljs-number">6</span> ),            <span class="hljs-comment">// (array) - use author id (available with Version 3.7).</span>
    <span class="hljs-string">'author__not_in'</span> => <span class="hljs-keyword">array</span>( <span class="hljs-number">2</span>, <span class="hljs-number">6</span> ),        <span class="hljs-comment">// (array)' - use author id (available with Version 3.7).</span>

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

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

<span class="hljs-comment">// Taxonomy Parameters - Show posts associated with certain taxonomy.</span>
<span class="hljs-comment">// http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters</span>
<span class="hljs-comment">// Important <span class="hljs-doctag">Note:</span> tax_query takes an array of tax query arguments arrays (it takes an array of arrays)</span>
<span class="hljs-comment">// 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.</span>
    <span class="hljs-string">'tax_query'</span> => <span class="hljs-keyword">array</span>(                     <span class="hljs-comment">// (array) - use taxonomy parameters (available with Version 3.1).</span>
    <span class="hljs-string">'relation'</span> => <span class="hljs-string">'AND'</span>,                      <span class="hljs-comment">// (string) - Possible values are 'AND' or 'OR' and is the equivalent of running a JOIN for each taxonomy</span>
      <span class="hljs-keyword">array</span>(
        <span class="hljs-string">'taxonomy'</span> => <span class="hljs-string">'color'</span>,                <span class="hljs-comment">// (string) - Taxonomy.</span>
        <span class="hljs-string">'field'</span> => <span class="hljs-string">'slug'</span>,                    <span class="hljs-comment">// (string) - Select taxonomy term by ('id' or 'slug')</span>
        <span class="hljs-string">'terms'</span> => <span class="hljs-keyword">array</span>( <span class="hljs-string">'red'</span>, <span class="hljs-string">'blue'</span> ),    <span class="hljs-comment">// (int/string/array) - Taxonomy term(s).</span>
        <span class="hljs-string">'include_children'</span> => <span class="hljs-literal">true</span>,           <span class="hljs-comment">// (bool) - Whether or not to include children for hierarchical taxonomies. Defaults to true.</span>
        <span class="hljs-string">'operator'</span> => <span class="hljs-string">'IN'</span>                    <span class="hljs-comment">// (string) - Operator to test. Possible values are 'IN', 'NOT IN', 'AND'.</span>
      ),
      <span class="hljs-keyword">array</span>(
        <span class="hljs-string">'taxonomy'</span> => <span class="hljs-string">'actor'</span>,
        <span class="hljs-string">'field'</span> => <span class="hljs-string">'id'</span>,
        <span class="hljs-string">'terms'</span> => <span class="hljs-keyword">array</span>( <span class="hljs-number">103</span>, <span class="hljs-number">115</span>, <span class="hljs-number">206</span> ),
        <span class="hljs-string">'include_children'</span> => <span class="hljs-literal">false</span>,
        <span class="hljs-string">'operator'</span> => <span class="hljs-string">'NOT IN'</span>
      )
    ),

<span class="hljs-comment">// Post & Page Parameters - Display content based on post and page parameters.</span>
<span class="hljs-comment">// http://codex.wordpress.org/Class_Reference/WP_Query#Post_.26_Page_Parameters</span>
    <span class="hljs-string">'p'</span> => <span class="hljs-number">1</span>,                               <span class="hljs-comment">// (int) - use post id.</span>
    <span class="hljs-string">'name'</span> => <span class="hljs-string">'hello-world'</span>,                <span class="hljs-comment">// (string) - use post slug.</span>
    <span class="hljs-string">'page_id'</span> => <span class="hljs-number">1</span>,                         <span class="hljs-comment">// (int) - use page id.</span>
    <span class="hljs-string">'pagename'</span> => <span class="hljs-string">'sample-page'</span>,            <span class="hljs-comment">// (string) - use page slug.</span>
    <span class="hljs-string">'pagename'</span> => <span class="hljs-string">'contact_us/canada'</span>,      <span class="hljs-comment">// (string) - Display child page using the slug of the parent and the child page, separated ba slash</span>
    <span class="hljs-string">'post_parent'</span> => <span class="hljs-number">1</span>,                     <span class="hljs-comment">// (int) - use page id. Return just the child Pages. (Only works with heirachical post types.)</span>
    <span class="hljs-string">'post_parent__in'</span> => <span class="hljs-keyword">array</span>(<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>)       <span class="hljs-comment">// (array) - use post ids. Specify posts whose parent is in an array. <span class="hljs-doctag">NOTE:</span> Introduced in 3.6</span>
    <span class="hljs-string">'post_parent__not_in'</span> => <span class="hljs-keyword">array</span>(<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>),  <span class="hljs-comment">// (array) - use post ids. Specify posts whose parent is not in an array.</span>
    <span class="hljs-string">'post__in'</span> => <span class="hljs-keyword">array</span>(<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>),             <span class="hljs-comment">// (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</span>
    <span class="hljs-string">'post__not_in'</span> => <span class="hljs-keyword">array</span>(<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>),         <span class="hljs-comment">// (array) - use post ids. Specify post NOT to retrieve.</span>
    <span class="hljs-comment">// <span class="hljs-doctag">NOTE:</span> you cannot combine 'post__in' and 'post__not_in' in the same query</span>

<span class="hljs-comment">// Password Parameters - Show content based on post and page parameters. Remember that default post_type is only set to display posts but not pages.</span>
<span class="hljs-comment">// http://codex.wordpress.org/Class_Reference/WP_Query#Password_Parameters</span>
    <span class="hljs-string">'has_password'</span> => <span class="hljs-literal">true</span>,                 <span class="hljs-comment">// (bool) - available with Version 3.9</span>
                                              <span class="hljs-comment">// true for posts with passwords;</span>
                                              <span class="hljs-comment">// false for posts without passwords;</span>
                                              <span class="hljs-comment">// null for all posts with and without passwords</span>
    <span class="hljs-string">'post_password'</span> => <span class="hljs-string">'multi-pass'</span>,          <span class="hljs-comment">// (string) - show posts with a particular password (available with Version 3.9)</span>

<span class="hljs-comment">//////Type & Status Parameters - Show posts associated with certain type or status.</span>
    <span class="hljs-comment">//http://codex.wordpress.org/Class_Reference/WP_Query#Type_Parameters</span>
    <span class="hljs-string">'post_type'</span> => <span class="hljs-keyword">array</span>(                   <span class="hljs-comment">//(string / array) - use post types. Retrieves posts by Post Types, default value is 'post';</span>
            <span class="hljs-string">'post'</span>,                         <span class="hljs-comment">// - a post.</span>
            <span class="hljs-string">'page'</span>,                         <span class="hljs-comment">// - a page.</span>
            <span class="hljs-string">'revision'</span>,                     <span class="hljs-comment">// - a revision.</span>
            <span class="hljs-string">'attachment'</span>,                   <span class="hljs-comment">// - 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'.</span>
            <span class="hljs-string">'my-post-type'</span>,                 <span class="hljs-comment">// - Custom Post Types (e.g. movies)</span>
            ),
    <span class="hljs-comment">//<span class="hljs-doctag">NOTE:</span> The 'any' keyword available to both post_type and post_status queries cannot be used within an array.</span>
    <span class="hljs-string">'post_type'</span> => <span class="hljs-string">'any'</span>,                   <span class="hljs-comment">// - retrieves any type except revisions and types with 'exclude_from_search' set to true.</span>

<span class="hljs-comment">//////Type & Status Parameters - Show posts associated with certain type or status.</span>
    <span class="hljs-comment">//http://codex.wordpress.org/Class_Reference/WP_Query#Status_Parameters</span>
    <span class="hljs-string">'post_status'</span> => <span class="hljs-keyword">array</span>(                 <span class="hljs-comment">//(string / array) - use post status. Retrieves posts by Post Status, default value i'publish'.</span>
            <span class="hljs-string">'publish'</span>,                      <span class="hljs-comment">// - a published post or page.</span>
            <span class="hljs-string">'pending'</span>,                      <span class="hljs-comment">// - post is pending review.</span>
            <span class="hljs-string">'draft'</span>,                        <span class="hljs-comment">// - a post in draft status.</span>
            <span class="hljs-string">'auto-draft'</span>,                   <span class="hljs-comment">// - a newly created post, with no content.</span>
            <span class="hljs-string">'future'</span>,                       <span class="hljs-comment">// - a post to publish in the future.</span>
            <span class="hljs-string">'private'</span>,                      <span class="hljs-comment">// - not visible to users who are not logged in.</span>
            <span class="hljs-string">'inherit'</span>,                      <span class="hljs-comment">// - a revision. see get_children.</span>
            <span class="hljs-string">'trash'</span>                         <span class="hljs-comment">// - post is in trashbin (available with Version 2.9).</span>
            ),
    <span class="hljs-comment">//<span class="hljs-doctag">NOTE:</span> The 'any' keyword available to both post_type and post_status queries cannot be used within an array.</span>
    <span class="hljs-string">'post_status'</span> => <span class="hljs-string">'any'</span>,                 <span class="hljs-comment">// - retrieves any status except those from post types with 'exclude_from_search' set to true.</span>

<span class="hljs-comment">//////Pagination Parameters</span>
    <span class="hljs-comment">//http://codex.wordpress.org/Class_Reference/WP_Query#Pagination_Parameters</span>
    <span class="hljs-string">'posts_per_page'</span> => <span class="hljs-number">10</span>,                 <span class="hljs-comment">//(int) - number of post to show per page (available with Version 2.1). Use 'posts_per_page'=1 to show all posts (the 'offset' parameter is ignored with a -1 value). 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</span>
    <span class="hljs-string">'posts_per_archive_page'</span> => <span class="hljs-number">10</span>,         <span class="hljs-comment">//(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</span>
    <span class="hljs-string">'nopaging'</span> => <span class="hljs-literal">false</span>,                    <span class="hljs-comment">//(bool) - show all posts or use pagination. Default value is 'false', use paging.</span>
    <span class="hljs-string">'paged'</span> => get_query_var(<span class="hljs-string">'paged'</span>),      <span class="hljs-comment">//(int) - number of page. Show the posts that would normally show up just on page X when usinthe "Older Entries" link.</span>
                                            <span class="hljs-comment">//<span class="hljs-doctag">NOTE:</span> 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 <!--nextpage--> Quicktag in the post content.</span>
                                            <span class="hljs-comment">// This whole paging thing gets tricky. Some links to help you out:</span>
                                            <span class="hljs-comment">// http://codex.wordpress.org/Function_Reference/next_posts_link#Usage_when_querying_the_loop_with_WP_Query</span>
                                            <span class="hljs-comment">// http://codex.wordpress.org/Pagination#Troubleshooting_Broken_Pagination</span>

    <span class="hljs-string">'offset'</span> => <span class="hljs-number">3</span>,                          <span class="hljs-comment">// (int) - number of post to displace or pass over.</span>
                                            <span class="hljs-comment">// 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</span>
                                            <span class="hljs-comment">// The 'offset' parameter is ignored when 'posts_per_page'=>-1 (show all posts) is used.</span>

    <span class="hljs-string">'page'</span> => get_query_var(<span class="hljs-string">'page'</span>),        <span class="hljs-comment">// (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.</span>
                                            <span class="hljs-comment">//<span class="hljs-doctag">NOTE:</span> The query variable 'page' holds the pagenumber for a single paginated Post or Page that includes the <!--nextpage--> Quicktag in the post content.</span>
    <span class="hljs-string">'ignore_sticky_posts'</span> => <span class="hljs-literal">false</span>,         <span class="hljs-comment">// (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. <span class="hljs-doctag">Note:</span> 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.</span>

<span class="hljs-comment">//////Order & Orderby Parameters - Sort retrieved posts.</span>
    <span class="hljs-comment">//http://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters</span>
    <span class="hljs-string">'order'</span> => <span class="hljs-string">'DESC'</span>,                      <span class="hljs-comment">//(string) - Designates the ascending or descending order of the 'orderby' parameter. Default to 'DESC'.</span>
                                              <span class="hljs-comment">//Possible Values:</span>
                                              <span class="hljs-comment">//'ASC' - ascending order from lowest to highest values (1, 2, 3; a, b, c).</span>
                                              <span class="hljs-comment">//'DESC' - descending order from highest to lowest values (3, 2, 1; c, b, a).</span>
    <span class="hljs-string">'orderby'</span> => <span class="hljs-string">'date'</span>,                    <span class="hljs-comment">//(string) - Sort retrieved posts by parameter. Defaults to 'date'. One or more options can be passed. EX: 'orderby' => 'menu_order title'</span>
                                              <span class="hljs-comment">//Possible Values:</span>
                                              <span class="hljs-comment">//'none' - No order (available with Version 2.8).</span>
                                              <span class="hljs-comment">//'ID' - Order by post id. Note the captialization.</span>
                                              <span class="hljs-comment">//'author' - Order by author.</span>
                                              <span class="hljs-comment">//'title' - Order by title.</span>
                                              <span class="hljs-comment">//'name' - Order by post name (post slug).</span>
                                              <span class="hljs-comment">//'date' - Order by date.</span>
                                              <span class="hljs-comment">//'modified' - Order by last modified date.</span>
                                              <span class="hljs-comment">//'parent' - Order by post/page parent id.</span>
                                              <span class="hljs-comment">//'rand' - Random order.</span>
                                              <span class="hljs-comment">//'comment_count' - Order by number of comments (available with Version 2.9).</span>
                                              <span class="hljs-comment">//'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).</span>
                                              <span class="hljs-comment">//'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).</span>
                                              <span class="hljs-comment">//'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'.</span>
                                              <span class="hljs-comment">//'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</span>
                                              <span class="hljs-comment">//'post__in' - Preserve post ID order given in the post__in array (available with Version 3.5).</span>

<span class="hljs-comment">//////Date Parameters - Show posts associated with a certain time and date period.</span>
    <span class="hljs-comment">//http://codex.wordpress.org/Class_Reference/WP_Query#Date_Parameters</span>
    <span class="hljs-string">'year'</span> => <span class="hljs-number">2014</span>,                         <span class="hljs-comment">//(int) - 4 digit year (e.g. 2011).</span>
    <span class="hljs-string">'monthnum'</span> => <span class="hljs-number">4</span>,                        <span class="hljs-comment">//(int) - Month number (from 1 to 12).</span>
    <span class="hljs-string">'w'</span> =>  <span class="hljs-number">25</span>,                             <span class="hljs-comment">//(int) - Week of the year (from 0 to 53). Uses the MySQL WEEK command. The mode is dependenon the "start_of_week" option.</span>
    <span class="hljs-string">'day'</span> => <span class="hljs-number">17</span>,                            <span class="hljs-comment">//(int) - Day of the month (from 1 to 31).</span>
    <span class="hljs-string">'hour'</span> => <span class="hljs-number">13</span>,                           <span class="hljs-comment">//(int) - Hour (from 0 to 23).</span>
    <span class="hljs-string">'minute'</span> => <span class="hljs-number">19</span>,                         <span class="hljs-comment">//(int) - Minute (from 0 to 60).</span>
    <span class="hljs-string">'second'</span> => <span class="hljs-number">30</span>,                         <span class="hljs-comment">//(int) - Second (0 to 60).</span>
    <span class="hljs-string">'m'</span> => <span class="hljs-number">201404</span>,                          <span class="hljs-comment">//(int) - YearMonth (For e.g.: 201307).</span>

    <span class="hljs-string">'date_query'</span> => <span class="hljs-keyword">array</span>(                  <span class="hljs-comment">//(array) - Date parameters (available with Version 3.7).</span>
                                              <span class="hljs-comment">//these are super powerful. check out the codex for more comprehensive code examples http://codex.wordpress.org/Class_Reference/WP_Query#Date_Parameters</span>
      <span class="hljs-keyword">array</span>(
        <span class="hljs-string">'year'</span> => <span class="hljs-number">2014</span>,                     <span class="hljs-comment">//(int) - 4 digit year (e.g. 2011).</span>
        <span class="hljs-string">'month'</span> => <span class="hljs-number">4</span>                        <span class="hljs-comment">//(int) - Month number (from 1 to 12).</span>
        <span class="hljs-string">'week'</span> => <span class="hljs-number">31</span>                        <span class="hljs-comment">//(int) - Week of the year (from 0 to 53).</span>
        <span class="hljs-string">'day'</span> => <span class="hljs-number">5</span>                          <span class="hljs-comment">//(int) - Day of the month (from 1 to 31).</span>
        <span class="hljs-string">'hour'</span> => <span class="hljs-number">2</span>                         <span class="hljs-comment">//(int) - Hour (from 0 to 23).</span>
        <span class="hljs-string">'minute'</span> => <span class="hljs-number">3</span>                       <span class="hljs-comment">//(int) - Minute (from 0 to 59).</span>
        <span class="hljs-string">'second'</span> => <span class="hljs-number">36</span>                      <span class="hljs-comment">//(int) - Second (0 to 59).</span>
        <span class="hljs-string">'after'</span>     => <span class="hljs-string">'January 1st, 2013'</span>, <span class="hljs-comment">//(string/array) - Date to retrieve posts after. Accepts strtotime()-compatible string, or array of 'year', 'month', 'day'</span>
        <span class="hljs-string">'before'</span>    => <span class="hljs-keyword">array</span>(               <span class="hljs-comment">//(string/array) - Date to retrieve posts after. Accepts strtotime()-compatible string, or array of 'year', 'month', 'day'</span>
          <span class="hljs-string">'year'</span>  => <span class="hljs-number">2013</span>,                  <span class="hljs-comment">//(string) Accepts any four-digit year. Default is empty.</span>
          <span class="hljs-string">'month'</span> => <span class="hljs-number">2</span>,                     <span class="hljs-comment">//(string) The month of the year. Accepts numbers 1-12. Default: 12.</span>
          <span class="hljs-string">'day'</span>   => <span class="hljs-number">28</span>,                    <span class="hljs-comment">//(string) The day of the month. Accepts numbers 1-31. Default: last day of month.</span>
        ),
        <span class="hljs-string">'inclusive'</span> => <span class="hljs-literal">true</span>,                <span class="hljs-comment">//(boolean) - For after/before, whether exact value should be matched or not'.</span>
        <span class="hljs-string">'compare'</span> =>  <span class="hljs-string">'='</span>,                  <span class="hljs-comment">//(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 '='</span>
        <span class="hljs-string">'column'</span> => <span class="hljs-string">'post_date'</span>,            <span class="hljs-comment">//(string) - Column to query against. Default: 'post_date'.</span>
        <span class="hljs-string">'relation'</span> => <span class="hljs-string">'AND'</span>,                <span class="hljs-comment">//(string) - OR or AND, how the sub-arrays should be compared. Default: AND.</span>
      ),
    ),

<span class="hljs-comment">//////Custom Field Parameters - Show posts associated with a certain custom field.</span>
    <span class="hljs-comment">//http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters</span>
    <span class="hljs-string">'meta_key'</span> => <span class="hljs-string">'key'</span>,                    <span class="hljs-comment">//(string) - Custom field key.</span>
    <span class="hljs-string">'meta_value'</span> => <span class="hljs-string">'value'</span>,                <span class="hljs-comment">//(string) - Custom field value.</span>
    <span class="hljs-string">'meta_value_num'</span> => <span class="hljs-number">10</span>,                 <span class="hljs-comment">//(number) - Custom field value.</span>
    <span class="hljs-string">'meta_compare'</span> => <span class="hljs-string">'='</span>,                  <span class="hljs-comment">//(string) - Operator to test the 'meta_value'. Possible values are '!=', '>', '>=', '<', or ='. Default value is '='.</span>
    <span class="hljs-string">'meta_query'</span> => <span class="hljs-keyword">array</span>(                  <span class="hljs-comment">//(array) - Custom field parameters (available with Version 3.1).</span>
       <span class="hljs-string">'relation'</span> => <span class="hljs-string">'AND'</span>,                 <span class="hljs-comment">//(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.</span>
       <span class="hljs-keyword">array</span>(
         <span class="hljs-string">'key'</span> => <span class="hljs-string">'color'</span>,                  <span class="hljs-comment">//(string) - Custom field key.</span>
         <span class="hljs-string">'value'</span> => <span class="hljs-string">'blue'</span>                  <span class="hljs-comment">//(string/array) - Custom field value (<span class="hljs-doctag">Note:</span> 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</span>
         <span class="hljs-string">'type'</span> => <span class="hljs-string">'CHAR'</span>,                  <span class="hljs-comment">//(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.</span>
                                            <span class="hljs-comment">//<span class="hljs-doctag">NOTE:</span> The 'type' DATE works with the 'compare' value BETWEEN only if the date is stored at the format YYYYMMDD and tested with this format.</span>
         <span class="hljs-string">'compare'</span> => <span class="hljs-string">'='</span>                   <span class="hljs-comment">//(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 '='.</span>
       ),
       <span class="hljs-keyword">array</span>(
         <span class="hljs-string">'key'</span> => <span class="hljs-string">'price'</span>,
         <span class="hljs-string">'value'</span> => <span class="hljs-keyword">array</span>( <span class="hljs-number">1</span>,<span class="hljs-number">200</span> ),
         <span class="hljs-string">'compare'</span> => <span class="hljs-string">'NOT LIKE'</span>
       )
    ),

<span class="hljs-comment">//////Permission Parameters - Display published posts, as well as private posts, if the user has the appropriate capability:</span>
    <span class="hljs-comment">//http://codex.wordpress.org/Class_Reference/WP_Query#Permission_Parameters</span>
    <span class="hljs-string">'perm'</span> => <span class="hljs-string">'readable'</span>                    <span class="hljs-comment">//(string) Possible values are 'readable', 'editable'</span>

<span class="hljs-comment">//////Caching Parameters</span>
    <span class="hljs-comment">//http://codex.wordpress.org/Class_Reference/WP_Query#Caching_Parameters</span>
    <span class="hljs-comment">//NOTE Caching is a good thing. Setting these to false is generally not advised.</span>
    <span class="hljs-string">'cache_results'</span> => <span class="hljs-literal">true</span>,                <span class="hljs-comment">//(bool) Default is true - Post information cache.</span>
    <span class="hljs-string">'update_post_term_cache'</span> => <span class="hljs-literal">true</span>,       <span class="hljs-comment">//(bool) Default is true - Post meta information cache.</span>
    <span class="hljs-string">'update_post_meta_cache'</span> => <span class="hljs-literal">true</span>,       <span class="hljs-comment">//(bool) Default is true - Post term information cache.</span>

    <span class="hljs-string">'no_found_rows'</span> => <span class="hljs-literal">false</span>,               <span class="hljs-comment">//(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</span>

<span class="hljs-comment">//////Search Parameter</span>
    <span class="hljs-comment">//http://codex.wordpress.org/Class_Reference/WP_Query#Search_Parameter</span>
    <span class="hljs-string">'s'</span> => $s,                              <span class="hljs-comment">//(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</span>
    <span class="hljs-string">'exact'</span> => <span class="hljs-literal">true</span>,                        <span class="hljs-comment">//(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</span>
    <span class="hljs-string">'sentence'</span> => <span class="hljs-literal">true</span>,                     <span class="hljs-comment">//(bool) - flag to make it do a phrase search - Default value is false. For more information see: https://gist.github.com/2023628#gistcomment-285118</span>

<span class="hljs-comment">//////Post Field Parameters</span>
    <span class="hljs-comment">//For more info see: http://codex.wordpress.org/Class_Reference/WP_Query#Return_Fields_Parameter</span>
    <span class="hljs-comment">//also https://gist.github.com/luetkemj/2023628/#comment-1003542</span>
    <span class="hljs-string">'fields'</span> => <span class="hljs-string">'ids'</span>                       <span class="hljs-comment">//(string) - Which fields to return. All fields are returned by default.</span>
                                              <span class="hljs-comment">//Possible values:</span>
                                              <span class="hljs-comment">//'ids'        - Return an array of post IDs.</span>
                                              <span class="hljs-comment">//'id=>parent' - Return an associative array [ parent => ID, тАж ].</span>
                                              <span class="hljs-comment">//Passing anything else will return all fields (default) - an array of post objects.</span>

<span class="hljs-comment">//////Filters</span>
    <span class="hljs-comment">//For more information on available Filters see: http://codex.wordpress.org/Class_Reference/WP_Query#Filters</span>

);

$the_query = <span class="hljs-keyword">new</span> WP_Query( $args );

<span class="hljs-comment">// The Loop</span>
<span class="hljs-keyword">if</span> ( $the_query->have_posts() ) {
    <span class="hljs-keyword">while</span> ( $the_query->have_posts() ) {
      $the_query->the_post();
      <span class="hljs-comment">// Do Stuff</span>
    } <span class="hljs-comment">// end while</span>
} <span class="hljs-comment">// endif</span>
<span class="hljs-comment">// Reset Post Data</span>
wp_reset_postdata();

Источник

Share this post: