/ Database / query.wp_query


Title : query.wp_query
Purpose : To fetch the posts.
Syntax : [query.wp_query o.exit=d]
“post_type”: “notification_tpl”
Input Parameters : The array of the args. you can pass JSON args or by creating PHP array and pass it to the args parameter.

Author Parameters:

Show posts associated with certain authors.

  • author (int) – use the author id.
  • author_name (string) – use ‘user_nicename‘ – NOT name.
  • author__in (array) – use author id
  • author__not_in (array) – use author id

Category Parameters

Show posts associated with certain categories.

  • 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.

Post & Page Parameters

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

  • 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.
  • post_parent__not_in (array) – use post ids. Specify posts whose parent is not in an array.
  • 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 behavior 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.

Post Type Parameters

Show posts associated with a certain type.

  • 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’. See Status parameters section below.
    • nav_menu_item‘ – a navigation menu item
    • any‘ – retrieves any type except revisions and types with ‘exclude_from_search’ set to true.
      ** Custom Post Types (e.g. movies)

Status Parameters

Show posts associated with certain post status.

  • post_status (string / array) – use post status. Retrieves posts by post status. The 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).

Comment Parameters

Since Version 4.9 Introduced the $comment_count parameter. It can be either an Integer or an Array.

  • comment_count (int) – The number of comments your CPT has to have ( Search operator will do a ‘=’ operation )
  • comment_count (Array) – If comment_count is an array, it should have two arguments:
    • value‘ – The number of comments your post has to have when comparing
    • compare‘ – The search operator. Possible values are ‘=’, ‘!=’, ‘>’, ‘>=’, ‘<‘, ‘<=’. The default value is ‘=’.

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).
    • relevance‘ – Order by search terms in the following order: First, whether the entire sentence is matched. Second, if all the search terms are within the titles. Third, if any of the search terms appear in the titles. And, fourth, if the full sentence appears in the contents.
    • 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‘. When using ‘meta_type’ you can also use ‘meta_value_*’ accordingly. For example, when using DATETIME as ‘meta_type’ you can use ‘meta_value_datetime’ to define order structure.
    • 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). Note – the value of the order parameter does not change the resulting sort order.
    • post_name__in‘ – Preserve post slug order given in the ‘post_name__in’ array (available since Version 4.6). Note – the value of the order parameter does not change the resulting sort order.
    • post_parent__in‘ -Preserve post parent order given in the ‘post_parent__in’ array (available since Version 4.6). Note – the value of the order parameter does not change the resulting sort order.
Return Value : WP_Query object
Example 1 :
Code : [query.wp_query o.exit=d]
“post_type”: “notification_tpl”,
“meta_query”: {
Output : WP_Query object in which we get notification_tpl’s posts.
Most Popular

Leave a Reply

Your email address will not be published.