本文约 4,702 字,预计阅读 12 分钟。文中命令与配置均经过实测,可直接参考执行。
引言:资讯站真正的瓶颈在后台,不在前台
资讯站的页面结构看起来很简单:头条、次条、列表、侧栏。但真正上线后你会发现,难的是每天要稳定产出 10–30 篇内容,还要保证不出现”昨天发过的稿子今天又发一遍””稿件没审就发出去了””专题页忘了更新”这类事故。这些事故都不是模板问题,是流程问题。所以搭资讯站要同时设计两套东西:一套是给读者看的信息流结构,一套是给编辑用的工作流。两套都设计好,站点才能持续跑。
原理分析:信息流不是一次查询能搞定的
新手常犯的错误是在首页模板里写 6 个 WP_Query,每个栏目一个。结果是首页 6 次数据库查询 + 每次查询的 meta 补全,TTFB 轻松超过 1 秒。正确做法是”一次主查询 + 分段渲染”:把首屏要展示的文章按栏目分组,用一个 WP_Query 配 tax_query 的 IN 条件一次性取出,再在 PHP 里按栏目分组渲染。
| 方案 | 查询次数 | 适用场景 |
|---|---|---|
| 每个栏目一次查询 | N 次 | 栏目 ≤ 3,流量小 |
| 一次查询 + 内存分组 | 1 次 | 栏目多,流量中高 |
| 全静态缓存首屏 | 0 次 | 流量大,更新频率固定 |
编辑流程方面,WordPress 默认的 draft → pending → publish 状态机对资讯站不够用。PublishPress 系列插件(原 PublishPress Planner、PublishPress Checklists、PublishPress Revisions)能补上:编辑日历看排期、发布前检查清单(是否配图、是否填摘要、是否选栏目)、以及已发布内容的修订流转。
保姆级实操
步骤 1:规划栏目结构。 资讯站的栏目建议控制在 6–8 个,超过这个数用户记不住,编辑也分不清。用分类(category)承载栏目,用标签(tag)承载临时话题。
步骤 2:首页一次性取数。 用 tax_query 的 IN 条件把多个栏目的文章一次取出来,同时用 posts_per_page 控制总量。
function mimangfei_homepage_feed() {
$cats = array( 3, 5, 8 ); // 头条、深度、快讯三个栏目 ID
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 30,
'no_found_rows' => true, // 首页不需要分页,省一次 COUNT
'ignore_sticky_posts' => true,
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => $cats,
'operator' => 'IN',
),
),
);
$posts = get_posts( $args );
// 在内存里按栏目分组
$grouped = array();
foreach ( $posts as $p ) {
$terms = get_the_terms( $p->ID, 'category' );
if ( empty( $terms ) || is_wp_error( $terms ) ) {
continue;
}
foreach ( $terms as $t ) {
if ( in_array( (int) $t->term_id, $cats, true ) ) {
$grouped[ $t->term_id ][] = $p;
}
}
}
return $grouped;
}
步骤 3:用 transient 缓存首屏。 资讯站首屏不需要实时,缓存 5 分钟能扛住绝大多数流量峰值,并在发布新稿时主动清除。
function mimangfei_get_feed_cached() {
$key = 'mmf_home_feed';
$data = get_transient( $key );
if ( false === $data ) {
$data = mimangfei_homepage_feed();
set_transient( $key, $data, 5 * MINUTE_IN_SECONDS );
}
return $data;
}
// 发布新文章时立即清掉首页缓存
add_action( 'transition_post_status', 'mimangfei_purge_feed_cache', 10, 3 );
function mimangfei_purge_feed_cache( $new_status, $old_status, $post ) {
if ( 'publish' === $new_status && 'publish' !== $old_status ) {
delete_transient( 'mmf_home_feed' );
}
}
步骤 4:处理置顶与头条。 用 WordPress 原生的 sticky 机制做头条,但记得在非首页场景 ignore_sticky_posts,否则归档页顺序会乱。
步骤 5:搭编辑流程。 安装 PublishPress Planner(原 Editorial Calendar 的进化版),在后台配置:编辑日历(拖拽排期)、发布检查清单(必须配图 / 必须填摘要 / 必须选栏目)、以及自定义状态(待采访 / 待编辑 / 待发布)。
步骤 6:给自定义状态流转加通知。 用 WordPress 原生的 transition_post_status 钩子,在状态变化时通知对应责任人。
add_action( 'transition_post_status', 'mimangfei_notify_editor', 10, 3 );
function mimangfei_notify_editor( $new_status, $old_status, $post ) {
if ( 'pending' === $new_status && 'draft' === $old_status ) {
$editor = get_user_by( 'email', get_option( 'admin_email' ) );
if ( $editor ) {
wp_mail(
$editor->user_email,
'有稿件待审:' . $post->post_title,
'预览地址:' . get_preview_post_link( $post->ID )
);
}
}
}
步骤 7:做去重与防重复发布。 资讯站最常见的编辑事故是同一篇稿子发两次。加一条标题查重。
add_action( 'save_post', 'mimangfei_prevent_duplicate', 10, 2 );
function mimangfei_prevent_duplicate( $post_id, $post ) {
if ( wp_is_post_revision( $post_id ) || 'publish' !== $post->post_status ) {
return;
}
$same = get_posts( array(
'title' => $post->post_title,
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 1,
'exclude' => array( $post_id ),
'fields' => 'ids',
) );
if ( ! empty( $same ) ) {
wp_mail( get_option( 'admin_email' ), '疑似重复发布', '与本文标题相同的已发布文章:' . get_permalink( $same[0] ) );
}
}
步骤 8:配置首页的分层缓存。 首屏用 transient,列表页用页面缓存,文章页用 CDN 缓存,三层的过期时间依次拉长。
常见踩坑
- 首页 6 次 WP_Query。 TTFB 超过 1 秒,改成一次查询后通常能降到 300ms 以内。
- 缓存没在发布时清除。 编辑发了稿,前台十分钟不更新,被投诉”不发稿”。
- 置顶文章污染归档页。 忘了在归档查询里加
ignore_sticky_posts。 - 案例: 某资讯站在世界杯期间做了一个专题,编辑在专题页手动维护”最新 10 条”列表,结果第二天忘了更新,读者在专题页看到的还是前一天的比分。教训:专题聚合必须是自动查询,不能手工维护列表。正确做法见专题聚合页自动收录。
- 栏目太多导致后台选错。 8 个以上栏目时,编辑发布时选错栏目的概率显著上升。
- 图片没做统一尺寸。 信息流里缩略图尺寸不一,列表页高度跳动,影响阅读体验。
- 没做分页的深度控制。 列表页分页无限深,搜索引擎抓取大量低价值分页,应在第 10 页后加
noindex。
总结
资讯站要同时设计前台结构与后台流程。前台:一次查询 + 内存分组 + transient 缓存首屏,发布时主动清除;后台:PublishPress 管排期与检查清单,用 transition_post_status 做通知,加标题查重防重复发布。判断标准:编辑从写稿到发布不需要在站外沟通,且发布后首屏 1 分钟内更新。专题页的自动聚合做法见专题页结构与收录优化;外贸资讯站的内容结构见外贸独立站搭建流程。
