query.get_posts

/ Database / query.get_posts

Title : query.get_posts
Purpose : Retrieves an array of the latest posts, or posts matching the given criteria.
Syntax : [query.get_posts post_type=’notification_tpl’ dump=d /]
Input Parameters :
  • ‘name’
    (string) Post slug.
  • ‘posts_per_page’
    (int) The number of posts to query for. Use -1 to request all posts.
  • ‘offset’
    (int) The number of posts to offset before retrieval.
  • ‘category_name’
    (string) Use category slug (not name, this or any children).
  • ‘order’
    (string) Designates ascending or descending order of posts. Default ‘DESC’. Accepts ‘ASC’, ‘DESC’.
  • ‘orderby’
    (string|array) Sort retrieved posts by parameter.
  • ‘meta_key’
    (string) Custom field key.
  • ‘meta_value’
    (string) Custom field value.
  • post_type’
    (string|array) A post type slug (string) or array of post type slugs. Default ‘any’ if using ‘tax_query’.
  • ‘post_status’
    (string|array) A post status (string) or array of post statuses.
  • ‘post_mime_type’
    (string) The mime type of the post. Used for ‘attachment’ post_type.
  • ‘post_parent’
    (int) Page ID to retrieve child pages for. Use 0 to only retrieve top-level pages.
  • ‘author’
    (int|string) Author ID, or comma-separated list of IDs.
  • ‘suppress_filters’
    (bool) Whether to suppress filters. Default false.
Return Value : Array of post objects or post IDs.
Example 1 :
Code : [query.get_posts post_type=’notification_tpl’ dump=d /]
Output : Array of post objects of notification tpl CPT
Description :
Retrieves an array of the latest posts, or posts matching the given criteria.

Second Approach:

  • You can use get_posts in json approach too.
  • which means you can pass all parameters in json
  • For example,
  • In above example, we are fetching posts from
    • post_type as NEWS
    • post_status as publish
    • order is descending and orderby date.
  • Use this in your code to make it more easy.
  • Happy Coding!
Categories
Most Popular

Leave a Reply

Your email address will not be published.