get_post() 函数详解
get_post()用于获取单篇文章的数据,返回一个 WP_Post 对象。
基本语法
$post = get_post( $post, $output, $filter );
参数说明
- $post (可选)
- 默认为全局
$post对象 - 可以是:文章ID、WP_Post 对象、null
- 默认为全局
- $output (可选)
- 返回值类型,默认:OBJECT
- 可选值:
OBJECT– 返回 WP_Post 对象(默认)ARRAY_A– 返回关联数组ARRAY_N– 返回数字索引数组
- $filter (可选)
- 过滤类型,默认:raw
- 可选值:
raw– 原始数据edit– 编辑时数据db– 数据库数据display– 显示时数据attribute– 转义为 HTML 属性js– 转义为 JavaScript
基本用法示例
示例1:获取当前文章
// 在循环内部或单篇文章页
global $post;
$current_post = get_post(); // 不传参数,获取当前全局文章
// 或
$current_post = get_post( get_the_ID() );
示例2:通过文章ID获取
$post_id = 123;
$post = get_post( $post_id );
if ( $post ) {
echo '文章标题:' . $post->post_title;
echo '文章内容:' . $post->post_content;
echo '作者ID:' . $post->post_author;
echo '发布时间:' . $post->post_date;
}
示例3:返回数组格式
$post = get_post( 123, ARRAY_A );
if ( $post ) {
echo '标题:' . $post['post_title'];
echo '状态:' . $post['post_status'];
echo '类型:' . $post['post_type'];
}
WP_Post 对象常用属性
$post = get_post( 123 );
// 核心属性
$post->ID; // 文章ID
$post->post_title; // 文章标题
$post->post_content; // 文章内容
$post->post_excerpt; // 文章摘要
$post->post_status; // 状态(publish, draft, pending等)
$post->post_type; // 文章类型(post, page, 自定义类型)
$post->post_author; // 作者ID
$post->post_date; // 发布日期
$post->post_modified; // 修改日期
$post->post_parent; // 父文章ID(用于页面层级)
$post->menu_order; // 菜单顺序
$post->comment_count; // 评论数量
$post->guid; // 文章全局唯一标识符
实际应用示例
示例1:获取文章及其元数据
$post_id = 456;
$post = get_post( $post_id );
if ( $post ) {
// 获取文章元数据
$custom_field = get_post_meta( $post_id, 'custom_field_name', true );
// 获取分类
$categories = get_the_category( $post_id );
// 获取标签
$tags = get_the_tags( $post_id );
// 获取作者信息
$author = get_userdata( $post->post_author );
$author_name = $author->display_name;
// 输出文章信息
echo '<h1>' . esc_html( $post->post_title ) . '</h1>';
echo '<div class="content">' . apply_filters( 'the_content', $post->post_content ) . '</div>';
}
示例2:自定义查询多篇文章
// 获取特定条件的多篇文章
$args = array(
'post_type' => 'post',
'posts_per_page' => 5,
'category_name' => 'news',
'orderby' => 'date',
'order' => 'DESC',
);
$posts = get_posts( $args );
if ( $posts ) {
foreach ( $posts as $post ) {
setup_postdata( $post );
?>
<article>
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<div class="excerpt"><?php the_excerpt(); ?></div>
</article>
<?php
}
wp_reset_postdata();
}
示例3:获取特定文章类型
// 获取页面
$page = get_post( 789 );
if ( $page && 'page' === $page->post_type ) {
echo '页面标题:' . $page->post_title;
}
// 获取自定义文章类型
$product = get_post( 999 );
if ( $product && 'product' === $product->post_type ) {
echo '产品名称:' . $product->post_title;
}
与相关函数的比较
| 函数 | 返回值 | 用途 | 示例 |
|---|---|---|---|
| get_post() | 单篇文章对象/数组 | 获取单篇文章 | $post = get_post(123); |
| get_posts() | 文章数组 | 获取多篇文章 | $posts = get_posts($args); |
| WP_Query | WP_Query对象 | 复杂查询 | $query = new WP_Query($args); |
| query_posts() | 修改主查询 | 修改主循环 | query_posts('cat=1'); |
最佳实践
- 错误处理
$post = get_post( $post_id );
if ( $post && 'publish' === $post->post_status ) {
// 处理已发布的文章
} else {
// 文章不存在或未发布
echo '文章不存在或未发布';
}
- 使用缓存
// WordPress 自带对象缓存
$post_id = 123;
$post = wp_cache_get( 'post_' . $post_id, 'posts' );
if ( false === $post ) {
$post = get_post( $post_id );
wp_cache_set( 'post_' . $post_id, $post, 'posts', 3600 ); // 缓存1小时
}
- 安全输出
$post = get_post( 123 );
if ( $post ) {
// 安全输出标题
echo esc_html( $post->post_title );
// 安全输出内容(保留HTML标签)
echo wp_kses_post( $post->post_content );
// 或使用WordPress内容过滤器
echo apply_filters( 'the_content', $post->post_content );
}
- 获取相邻文章
// 上一篇
$prev_post = get_previous_post();
if ( $prev_post ) {
$prev_post = get_post( $prev_post->ID );
echo '上一篇:<a href="' . get_permalink( $prev_post->ID ) . '">' . $prev_post->post_title . '</a>';
}
// 下一篇
$next_post = get_next_post();
if ( $next_post ) {
$next_post = get_post( $next_post->ID );
echo '下一篇:<a href="' . get_permalink( $next_post->ID ) . '">' . $next_post->post_title . '</a>';
}
常见问题
- 为什么 get_post() 返回 null?
- 文章ID不存在
- 文章状态不是发布状态
- 没有传入正确的参数
- 如何检查文章是否存在?
$post = get_post( 123 );
if ( null !== $post ) {
// 文章存在
}
- 如何获取修订版本?
$revisions = wp_get_post_revisions( $post_id );
if ( $revisions ) {
$latest_revision = array_shift( $revisions );
$revision_post = get_post( $latest_revision->ID );
}
get_post()是 WordPress 开发中最基础和最重要的函数之一,熟练掌握它对于主题和插件开发至关重要。


湘公网安备43020002000238