WordPress V5.0+Template Tag Reference Guide as of Version 5.2
Presented by DBS > Interactive Development :
Query Tags
Get Postsget_posts();
This is a simple function for creating multiple loops. It retrieves a list of latest posts or posts matching criteria. Arguments are passed in as either an array or a query string.
$args = array( | ||||
'numberposts' | => | 5, | ||
'offset' | => | 0, | ||
'category' | => | '', | ||
'category_name' | => | '', | ||
'tag' | => | '', | ||
'orderby' | => | 'post_date', | ||
'order' | => | 'DESC', | ||
'include' | => | '', | ||
'exclude' | => | '', | ||
'meta_key' | => | '', | ||
'meta_value' | => | '', | ||
'post_type' | => | 'publish', | ||
'post_status' | => | '', | ||
'post_parent' | => | '', | ||
'nopaging' | => | '' | ||
); |
<?php get_posts($args); ?>
numberposts
Integer • Default: 5
(Optional) Number of posts to return. Set to 0 to use the max number of posts per page. Set to -1 to remove the limit.
offset
Integer • Default: 0
(Optional) Offset from latest post.
category
Integer • Default: none
(Optional) Only show posts from this category ID. Making the category ID negative (-3 rather than 3) will show results not matching that category ID. Multiple category IDs can be specified by separating the category IDs with commas - but an array of IDs does not work.
category_name
String • Default: none
(Optional) Only show posts from this category name or category slug.
tag
String • Default: none
(Optional) Only show posts with this tag slug. If you specify multiple tag slugs separated by commas, all results matching any tag will be returned. If you specify multiple tag slugs separated by spaces, the results will match all the specified tag slugs.
orderby
String • Default: 'post_date'
(Optional) Sort posts by one of various values (separated by space), including:
- 'author' - Sort by the numeric author IDs.
- 'category' - Sort by the numeric category IDs.
- 'content' - Sort by content.
- 'date' - Sort by creation date.
- 'ID' - Sort by numeric post ID.
- 'menu_order' - Sort by the menu order. Only useful with pages and attachments.
- 'mime_type' - Sort by MIME type. Only useful with attachments.
- 'modified' - Sort by last modified date.
- 'name' - Sort by stub.
- 'parent' - Sort by parent ID.
- 'password' - Sort by password.
- 'rand' - Randomly sort results.
- 'status' - Sort by status.
- 'title' - Sort by title.
- 'type' - Sort by type.
order
String • Default: 'DESC'
(Optional) How to sort $orderby. Valid values: 'ASC' - Ascending (lowest to highest) 'DESC' - Descending (highest to lowest)
include
String • Default: none
(Optional) The IDs of the posts you want to show, separated by commas and/or spaces. The following value would work in showing these six posts: '45, 63, 78, 94, 128, 140'. Note: Using this parameter will override the numberposts, offset, category, exclude, meta_key, meta_value, and post_parent parameters.
exclude
String • Default: none
(Optional) The IDs of any posts you want to exclude, separated by commas and/or spaces (see $include parameter).
meta_key and meta_value
String • Default: none
(Optional) Only show posts that contain a meta (custom) field with this key and value. Both parameters must be defined, or neither will work.
post_type
String • Default: 'post'
(Optional) The type of post to show. Available options are:
- 'post'
- 'page'
- 'attachment'
- 'any' - all post types
post_status
String • Default: 'publish'
(Optional) Show posts with a particular status. Multiple statuses can be given, separated with a comma. Available options are:
- 'publish'
- 'private'
- 'draft'
- 'future'
- 'inherit' - Default if $post_type is set to attachment
- 'any' - all statuses
post_parent
Integer • Default: none
(Optional) Show only the children of the post with this ID.
nopaging
Boolean • Default: none
(Optional) Enable or disable paging. If paging is disabled, the numberposts option is ignored.
WP Reset Querywp_reset_query();
This function destroys the previous query used on a custom Loop. Function should be called after The Loop to ensure conditional tags work as expected.
<?php wp_reset_query(); ?>
No parameters.