get_postdata()
云策文档标注
概述
get_postdata() 函数用于获取指定文章的所有数据,但自 WordPress 1.5.1 版本起已被弃用,建议使用 get_post() 替代。
关键要点
- 函数功能:通过文章 ID 检索文章数据,返回包含 ID、作者、日期、内容等字段的数组。
- 弃用状态:自 WordPress 1.5.1 版本起标记为弃用,推荐使用 get_post() 函数。
- 参数要求:必需参数 $postid,类型为整数,表示文章 ID。
- 返回值:返回文章数据的数组,格式与 get_post() 类似但字段名略有不同。
代码示例
function get_postdata($postid) {
_deprecated_function( __FUNCTION__, '1.5.1', 'get_post()' );
$post = get_post($postid);
$postdata = array (
'ID' => $post->ID,
'Author_ID' => $post->post_author,
'Date' => $post->post_date,
'Content' => $post->post_content,
'Excerpt' => $post->post_excerpt,
'Title' => $post->post_title,
'Category' => $post->post_category,
'post_status' => $post->post_status,
'comment_status' => $post->comment_status,
'ping_status' => $post->ping_status,
'post_password' => $post->post_password,
'to_ping' => $post->to_ping,
'pinged' => $post->pinged,
'post_type' => $post->post_type,
'post_name' => $post->post_name
);
return $postdata;
}注意事项
- 此函数已弃用,新代码中应避免使用,改用 get_post() 以保持兼容性和最佳实践。
- 函数内部调用 _deprecated_function() 来标记弃用状态,使用时可能会触发相关警告。
原文内容
Retrieves all post data for a given post.
Description
See also
Parameters
$postidintrequired-
Post ID.
Source
function get_postdata($postid) {
_deprecated_function( __FUNCTION__, '1.5.1', 'get_post()' );
$post = get_post($postid);
$postdata = array (
'ID' => $post->ID,
'Author_ID' => $post->post_author,
'Date' => $post->post_date,
'Content' => $post->post_content,
'Excerpt' => $post->post_excerpt,
'Title' => $post->post_title,
'Category' => $post->post_category,
'post_status' => $post->post_status,
'comment_status' => $post->comment_status,
'ping_status' => $post->ping_status,
'post_password' => $post->post_password,
'to_ping' => $post->to_ping,
'pinged' => $post->pinged,
'post_type' => $post->post_type,
'post_name' => $post->post_name
);
return $postdata;
}
Changelog
| Version | Description |
|---|---|
| 1.5.1 | Deprecated. Use get_post() |
| 0.71 | Introduced. |