You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:使用wp_insert_post时同步插表及实现关注用户新文章通知

嘿,这两个需求都可以通过WordPress的钩子系统完美解决,我给你拆解成两个部分来说明:

需求一:插入新文章时同步添加自定义表记录

WordPress在文章保存完成后会触发save_post钩子,我们可以利用这个钩子来同步操作自定义表。这里要注意区分新增文章和更新文章,避免重复插入记录,还要跳过自动草稿的情况。

示例代码如下:

add_action('save_post', 'sync_custom_table_on_post_insert', 10, 3);
function sync_custom_table_on_post_insert($post_id, $post, $update) {
    // 跳过自动草稿、修订版本和非公开文章
    if (wp_is_post_autosave($post_id) || wp_is_post_revision($post_id) || $post->post_status !== 'publish') {
        return;
    }

    // 只处理新增文章,不是更新操作
    if (!$update) {
        global $wpdb;
        $custom_table = $wpdb->prefix . 'your_custom_table'; // 替换成你的自定义表名

        // 准备要插入的数据,记得做 sanitize 防止SQL注入
        $data = array(
            'post_id' => intval($post_id),
            'author_id' => intval($post->post_author),
            'created_at' => current_time('mysql') // WordPress标准时间格式
            // 其他你需要的字段
        );

        // 数据格式定义
        $format = array('%d', '%d', '%s');

        // 执行插入
        $wpdb->insert($custom_table, $data, $format);
    }
}

关键点说明:

  • 用$update参数判断是否是新增文章(!$update表示新增)
  • 一定要对数据做 sanitize,比如用intval()转数字,current_time()获取标准时间
  • 替换your_custom_table为你实际的自定义表名,确保表结构和插入字段匹配
需求二:发布文章时向关注者推送通知(插入关联表记录)

这个需求同样基于save_post钩子,但需要额外做两个判断:一是文章状态变为publish,二是要获取所有关注当前作者的用户,然后批量插入通知记录。

示例代码:

add_action('save_post', 'send_notification_to_followers_on_publish', 10, 3);
function send_notification_to_followers_on_publish($post_id, $post, $update) {
    // 跳过自动草稿、修订版本
    if (wp_is_post_autosave($post_id) || wp_is_post_revision($post_id)) {
        return;
    }

    // 只处理刚发布的文章(新增发布或从草稿/待审转为发布)
    if (($post->post_status === 'publish' && !$update) || ($update && get_post_status($post_id) !== 'publish' && $post->post_status === 'publish')) {
        $author_id = intval($post->post_author);
        global $wpdb;

        // 第一步:查询所有关注该作者的用户ID(假设你的关注表是wp_followers,结构为follower_id, followed_id)
        $followers = $wpdb->get_col($wpdb->prepare(
            "SELECT follower_id FROM {$wpdb->prefix}followers WHERE followed_id = %d",
            $author_id
        ));

        if (!empty($followers)) {
            $notification_table = $wpdb->prefix . 'notifications'; // 替换成你的通知表名
            $notification_data = array();

            // 整理批量插入的数据
            foreach ($followers as $follower_id) {
                $notification_data[] = array(
                    'user_id' => intval($follower_id),
                    'post_id' => intval($post_id),
                    'message' => '你关注的作者发布了新文章:' . wp_strip_all_tags($post->post_title),
                    'created_at' => current_time('mysql'),
                    'status' => 'unread' // 未读状态
                );
            }

            // 批量插入,比循环单次插入更高效
            if (!empty($notification_data)) {
                $wpdb->insert_multiple($notification_table, $notification_data, array('%d', '%d', '%s', '%s', '%s'));
            }
        }
    }
}

关键点说明:

  • 判断条件($post->post_status === 'publish' && !$update) || ($update && get_post_status($post_id) !== 'publish' && $post->post_status === 'publish')确保只在文章首次发布或从其他状态转为发布时触发
  • 用$wpdb->get_col()获取关注者ID列表,避免多余数据
  • 批量插入用insert_multiple比循环insert性能更好,尤其是关注者较多时
  • 替换wp_followers和notifications为你实际的表名,确保表结构匹配

最后提醒一下:

  • 测试前记得备份数据库,避免操作失误导致数据丢失
  • 如果是多站点,可能需要额外处理站点ID的问题
  • 可以根据需求添加错误处理,比如判断$wpdb->insert是否成功

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 07:50:59