You need to enable JavaScript to run this app. 如何修改PHP snippet使其单次运行批量更新自定义文章摘要
优惠活动
大模型
产品
解决方案
定价
更多

如何修改PHP snippet使其单次运行批量更新自定义文章摘要

自定义文章摘要批量更新的自运行自销毁方案

核心调整思路

基于你现有单篇保存的逻辑,可以把代码改造成访问指定管理链接触发、分批处理所有目标文章、执行完成后自动移除自身代码块的一次性脚本,完全不需要逐篇编辑内容。
首先修正你原有代码的潜在bug:原单篇回调里写了get_post_type( $post->ID ),但回调传入的参数只有$post_id,根本没有定义$post变量,之前能跑是依赖全局$post变量碰巧存在,批量执行时一定会报错,必须改成直接用传入的$post_id判断。

可直接使用的完整代码

// 保留原有单篇保存时自动更新摘要的逻辑,修正冗余问题
add_action('acf/save_post', function($post_id) {
    // 跳过自动保存、文章 revision 的无意义场景
    if (wp_is_post_autosave($post_id) || wp_is_post_revision($post_id)) return;
    // 修正原代码的变量错误,直接用传入的$post_id判断文章类型
    if (get_post_type($post_id) !== 'ultratech_product') return;

    $post_excerpt = get_field('short_description', $post_id);
    if (!empty($post_excerpt)) {
        $post_excerpt = strip_shortcodes($post_excerpt);
        $post_excerpt = apply_filters('the_content', $post_excerpt);
        $excerpt_length = 250; // 保留原有250字摘要长度设置
        $excerpt_more = apply_filters('excerpt_more', " " . '[...]');
        $post_excerpt = wp_trim_words($post_excerpt, $excerpt_length, $excerpt_more);
    }

    wp_update_post([
        'ID' => $post_id,
        'post_excerpt' => $post_excerpt
    ]);
}, 40);

// 新增:一次性批量执行的自销毁触发逻辑
add_action('admin_init', function() {
    // 仅允许站点管理员触发,避免非授权访问执行批量操作
    if (!current_user_can('manage_options')) return;
    // 校验指定触发参数,避免误触发
    if (!isset($_GET['run_excerpt_sync']) || $_GET['run_excerpt_sync'] !== 'ultratech') return;

    // 适配数千篇文章的处理规模,解除执行超时、内存限制
    set_time_limit(0);
    ini_set('memory_limit', '512M');

    $processed_count = 0;
    $batch_size = 100; // 每批处理100篇,避免一次性拉取全部文章导致超时
    $paged = 1;

    // 循环分批拉取所有目标文章,直到处理完全部内容
    while (true) {
        $query = new WP_Query([
            'post_type' => 'ultratech_product',
            'post_status' => 'any',
            'posts_per_page' => $batch_size,
            'paged' => $paged,
            'fields' => 'ids',
        ]);

        if (empty($query->posts)) break;

        foreach ($query->posts as $post_id) {
            // 完全复用原有单篇的摘要生成逻辑,保证规则一致
            $post_excerpt = get_field('short_description', $post_id);
            if (!empty($post_excerpt)) {
                $post_excerpt = strip_shortcodes($post_excerpt);
                $post_excerpt = apply_filters('the_content', $post_excerpt);
                $excerpt_length = 250;
                $excerpt_more = apply_filters('excerpt_more', " " . '[...]');
                $post_excerpt = wp_trim_words($post_excerpt, $excerpt_length, $excerpt_more);
            }

            wp_update_post([
                'ID' => $post_id,
                'post_excerpt' => $post_excerpt
            ]);
            $processed_count++;
        }

        // 处理完最后一批内容时退出循环
        if ($paged >= $query->max_num_pages) break;
        $paged++;
    }

    // 全部流程执行完成后,移除当前批量触发的钩子,实现自销毁
    remove_action('admin_init', __FUNCTION__);

    // 输出完成提示
    wp_die(sprintf('摘要更新完成,共处理 %d 篇产品文章,触发逻辑已自动移除。', $processed_count));
});

执行步骤

  • 把上面的代码粘贴到主题的functions.php或者你正在使用的自定义功能插件文件中保存
  • 用站点管理员账号登录后台,访问任意后台页面,在URL后追加参数?run_excerpt_sync=ultratech即可触发(例如https://你的站点域名/wp-admin/?run_excerpt_sync=ultratech)
  • 页面加载后会自动分批处理所有ultratech_product类型的文章,按照原ACF短描述字段的规则生成摘要,全部跑完会弹出完成提示,同时代码会自动把这个批量运行的钩子移除,不会残留冗余逻辑,之后哪怕再访问带参数的链接也不会重复执行。

注意事项

  • 代码默认每批处理100篇,如果你的主机PHP执行时间限制比较严格,可以把$batch_size的数值改小到50,避免执行中断
  • 如果处理过程中因为网络或者超时中断,重新访问带参数的触发链接即可,代码会重新从头处理所有文章,不会出现重复写入的副作用
  • 原单篇保存时的自动更新逻辑完全保留,之后你每新增或编辑产品文章时,还是会自动同步更新摘要

内容的提问来源于stack exchange,提问作者Zuggle

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.06 21:45:01