WordPress获取今日之前最后发布的文章ID实现代码示例
代码
//用于更新当天文章:获取今日之前最后发布的文章ID的封装函数
//这里是获取法函数
function zzw_get_today_start_post_id() {
global $wpdb;
// 获取站点时区的今天00:00:00时间
$today_start = current_time('Y-m-d 00:00:00');
// 精确获取所有公开文章类型(包括post/page/自定义类型)
$post_types = array_merge(
['post', 'page'],
get_post_types([
'public' => true,
'_builtin' => false
])
);
// 安全处理文章类型
$post_types_placeholder = implode(',', array_fill(0, count($post_types), '%s'));
// 高性能SQL查询(使用索引优化)
$query = $wpdb->prepare(
"SELECT ID
FROM {$wpdb->posts}
WHERE post_status = 'publish'
AND post_type IN ({$post_types_placeholder})
AND post_date < %s
ORDER BY post_date DESC
LIMIT 1",
array_merge($post_types, [$today_start])
);
$last_post_id = (int) $wpdb->get_var($query);
return $last_post_id;
}
//这里是延伸:赋值给 diy name 字段
function zzw_update_today_startpostid()
{
$last_post_id = (string)zzw_get_today_start_post_id();
update_option( 'diy name', $last_post_id );
}
代码使用:
使用重要事项:将 diy name 该为你的自定义字段名称
在你需要执行的位置添加 zzw_update_today_startpostid() 即可。
详解
这段代码包含两个函数,用于获取并存储今日之前最后发布的文章ID。下面是对代码的详细解析:
函数一:zzw_get_today_start_post_id()
作用:高效查询今日00:00:00之前发布的最后一篇文章的ID(支持所有公开文章类型)。
代码解析:
- 全局数据库对象
global $wpdb;
使用WordPress的数据库对象进行查询。
- 获取当天起始时间
$today_start = current_time('Y-m-d 00:00:00');
current_time()根据WordPress站点时区返回当前时间- 格式化为当天的00:00:00(如
2025-07-31 00:00:00)
- 获取所有公开文章类型
$post_types = array_merge(
['post', 'page'],
get_post_types([
'public' => true,
'_builtin' => false
])
);
- 包含默认文章类型
post和page - 动态获取所有自定义公开文章类型(如产品、项目等)
- 构建安全的SQL占位符
$post_types_placeholder = implode(',', array_fill(0, count($post_types), '%s'));
生成与文章类型数量匹配的占位符(如 '%s','%s','%s'),防止SQL注入。
- 执行优化查询
$query = $wpdb->prepare(
"SELECT ID
FROM {$wpdb->posts}
WHERE post_status = 'publish'
AND post_type IN ({$post_types_placeholder})
AND post_date < %s
ORDER BY post_date DESC
LIMIT 1",
array_merge($post_types, [$today_start])
);
- 条件:
- 文章状态为已发布 (
publish) - 文章类型在指定范围内
- 发布时间早于今日00:00:00
- 文章状态为已发布 (
- 排序:按发布时间倒序(获取最新的一篇)
- 限制:仅返回1条结果
- 安全性:使用
prepare()动态绑定参数
- 返回结果
$last_post_id = (int) $wpdb->get_var($query);
return $last_post_id;
将查询结果转为整数并返回(无结果时返回 0)
函数二:zzw_update_today_startpostid()
作用:将获取的文章ID存储到WordPress选项表中。
代码解析:
function zzw_update_today_startpostid() {
$last_post_id = (string)zzw_get_today_start_post_id();
update_option( 'diy name', $last_post_id );
}
- 调用
zzw_get_today_start_post_id()获取文章ID - 将ID转为字符串(WordPress选项值通常按字符串存储)
- 使用
update_option()将值保存到名为diy name的选项中(自动创建/更新)
关键特性总结
| 特性 | 说明 |
|---|---|
| 时区处理 | 使用 current_time() 确保符合站点时区设置 |
| 全面文章类型 | 覆盖所有公开类型(内置+自定义) |
| 高性能查询 | 通过 ORDER BY post_date DESC + LIMIT 1 利用数据库索引优化 |
| 防SQL注入 | prepare() + 参数绑定确保查询安全 |
| 数据存储 | 结果持久化到选项表,全局可用 |
使用场景示例
假设需要在首页显示”昨日最新文章”:
$yesterdays_last_id = get_option('diy name');
if ($yesterdays_last_id) {
$post = get_post($yesterdays_last_id);
echo '昨日头条:' . $post->post_title;
}
优化建议
- 选项命名
将'diy name'改为更具描述性的名称(如'zzw_last_pre_today_post_id'),避免命名冲突。 - 错误处理
添加查询结果验证:
if (false === $last_post_id) {
// 记录数据库错误
}
- 缓存机制
对查询结果进行缓存(如使用wp_cache),减少数据库请求:
$cache_key = 'zzw_today_start_post_id';
if (false === ($id = wp_cache_get($cache_key))) {
$id = zzw_get_today_start_post_id();
wp_cache_set($cache_key, $id, '', 12 * HOUR_IN_SECONDS);
}
return $id;
- 定时任务
结合WordPress Cron 每日自动更新:
add_action('zzw_daily_update', 'zzw_update_today_startpostid');
// 在主题/插件激活时设置定时任务
if (!wp_next_scheduled('zzw_daily_update')) {
wp_schedule_event(strtotime('tomorrow 00:05'), 'daily', 'zzw_daily_update');
}
这段代码通过严谨的参数处理和高效的SQL查询,实现了跨文章类型的”今日前最新文章ID”获取与存储功能,适合需要时间边界文章查询的场景。

