get_posts

函式
get_posts ( $args = null )
引數
  • (array) $args { Optional. Arguments to retrieve posts. See WP_Query::parse_query() for all available arguments. @type int $numberposts Total number of posts to retrieve. Is an alias of `$posts_per_page` in WP_Query. Accepts -1 for all. Default 5. @type int|string $category Category ID or comma-separated list of IDs (this or any children). Is an alias of `$cat` in WP_Query. Default 0. @type int[] $include An array of post IDs to retrieve, sticky posts will be included. Is an alias of `$post__in` in WP_Query. Default empty array. @type int[] $exclude An array of post IDs not to retrieve. Default empty array. @type bool $suppress_filters Whether to suppress filters. Default true. }
    Required:
    Default: null
返回值
  • (WP_Post[]|int[]) Array of post objects or post IDs.
相關
  • WP_Query
  • WP_Query::parse_query()
定義位置
相關方法
get_postnext_postsget_post_typesedit_postget_post_class
引入
1.2.0
棄用
-

get_posts函式是一個WordPress的函式,它根據指定的引數檢索一個陣列的文章: 這個函式以一個查詢引數陣列為引數,並返回一個文章物件的陣列。

檢索最新的文章,或符合給定條件的文章陣列。

關於接受的引數的更多資訊,請參見開發手冊中的{@link WP_Query}文件。

`$ignore_sticky_posts`和`$no_found_rows`引數會被這個函式忽略,並且都被設定為`true`。

預設值如下。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function get_posts( $args = null ) {
$defaults = array(
'numberposts' => 5,
'category' => 0,
'orderby' => 'date',
'order' => 'DESC',
'include' => array(),
'exclude' => array(),
'meta_key' => '',
'meta_value' => '',
'post_type' => 'post',
'suppress_filters' => true,
);
$parsed_args = wp_parse_args( $args, $defaults );
if ( empty( $parsed_args['post_status'] ) ) {
$parsed_args['post_status'] = ( 'attachment' === $parsed_args['post_type'] ) ? 'inherit' : 'publish';
}
if ( ! empty( $parsed_args['numberposts'] ) && empty( $parsed_args['posts_per_page'] ) ) {
$parsed_args['posts_per_page'] = $parsed_args['numberposts'];
}
if ( ! empty( $parsed_args['category'] ) ) {
$parsed_args['cat'] = $parsed_args['category'];
}
if ( ! empty( $parsed_args['include'] ) ) {
$incposts = wp_parse_id_list( $parsed_args['include'] );
$parsed_args['posts_per_page'] = count( $incposts ); // Only the number of posts included.
$parsed_args['post__in'] = $incposts;
} elseif ( ! empty( $parsed_args['exclude'] ) ) {
$parsed_args['post__not_in'] = wp_parse_id_list( $parsed_args['exclude'] );
}
$parsed_args['ignore_sticky_posts'] = true;
$parsed_args['no_found_rows'] = true;
$get_posts = new WP_Query;
return $get_posts->query( $parsed_args );
}
function get_posts( $args = null ) { $defaults = array( 'numberposts' => 5, 'category' => 0, 'orderby' => 'date', 'order' => 'DESC', 'include' => array(), 'exclude' => array(), 'meta_key' => '', 'meta_value' => '', 'post_type' => 'post', 'suppress_filters' => true, ); $parsed_args = wp_parse_args( $args, $defaults ); if ( empty( $parsed_args['post_status'] ) ) { $parsed_args['post_status'] = ( 'attachment' === $parsed_args['post_type'] ) ? 'inherit' : 'publish'; } if ( ! empty( $parsed_args['numberposts'] ) && empty( $parsed_args['posts_per_page'] ) ) { $parsed_args['posts_per_page'] = $parsed_args['numberposts']; } if ( ! empty( $parsed_args['category'] ) ) { $parsed_args['cat'] = $parsed_args['category']; } if ( ! empty( $parsed_args['include'] ) ) { $incposts = wp_parse_id_list( $parsed_args['include'] ); $parsed_args['posts_per_page'] = count( $incposts ); // Only the number of posts included. $parsed_args['post__in'] = $incposts; } elseif ( ! empty( $parsed_args['exclude'] ) ) { $parsed_args['post__not_in'] = wp_parse_id_list( $parsed_args['exclude'] ); } $parsed_args['ignore_sticky_posts'] = true; $parsed_args['no_found_rows'] = true; $get_posts = new WP_Query; return $get_posts->query( $parsed_args ); }
function get_posts( $args = null ) {
	$defaults = array(
		'numberposts'      => 5,
		'category'         => 0,
		'orderby'          => 'date',
		'order'            => 'DESC',
		'include'          => array(),
		'exclude'          => array(),
		'meta_key'         => '',
		'meta_value'       => '',
		'post_type'        => 'post',
		'suppress_filters' => true,
	);

	$parsed_args = wp_parse_args( $args, $defaults );
	if ( empty( $parsed_args['post_status'] ) ) {
		$parsed_args['post_status'] = ( 'attachment' === $parsed_args['post_type'] ) ? 'inherit' : 'publish';
	}
	if ( ! empty( $parsed_args['numberposts'] ) && empty( $parsed_args['posts_per_page'] ) ) {
		$parsed_args['posts_per_page'] = $parsed_args['numberposts'];
	}
	if ( ! empty( $parsed_args['category'] ) ) {
		$parsed_args['cat'] = $parsed_args['category'];
	}
	if ( ! empty( $parsed_args['include'] ) ) {
		$incposts                      = wp_parse_id_list( $parsed_args['include'] );
		$parsed_args['posts_per_page'] = count( $incposts );  // Only the number of posts included.
		$parsed_args['post__in']       = $incposts;
	} elseif ( ! empty( $parsed_args['exclude'] ) ) {
		$parsed_args['post__not_in'] = wp_parse_id_list( $parsed_args['exclude'] );
	}

	$parsed_args['ignore_sticky_posts'] = true;
	$parsed_args['no_found_rows']       = true;

	$get_posts = new WP_Query;
	return $get_posts->query( $parsed_args );

}

常見問題

FAQs
檢視更多 >