/ Database / query.posts_builder


Title : query.posts_builder
Purpose : Get posts as per mentioned args
Syntax :
Input Parameters : There are 4 parts for this builder:

  • Start part – post specifications
    1. Author Parameters

      Show posts associated with certain author.

      • author (int) – use author id.
      • author_name (string) – use ‘user_nicename‘ – NOT name.
      • author__in (array) – use author id
      • author__not_in (array) – use author id
    2. 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.
    3. 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.
    4. 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 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.
    5. Post Type Parameters
      Show posts associated with 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.
    6. 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.
  • Tax query –  taxonomy parameters

    Show posts associated with certain taxonomy.

    • 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’.
  • Meta query –  Meta parameters
    • 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'.
    • 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’.
Return Value : it will return wp query result
Example 1 :
Code :
Output :
Most Popular

Leave a Reply

Your email address will not be published. Required fields are marked *