How/where 当在 WordPress 中发布新的 post 时,您会向不同的 table 添加自定义 INSERT MySQL 查询吗?

How/where would you add a custom INSERT MySQL query to different table for when a new post is published in WordPress?

我正在尝试将新发布的 post 的记录放在单独的数据库 table 中,除了 post 发布的常规流程。

我尝试进入 wp-includes 文件夹中的 post.php,并在 function wp_insert_post() 中添加查询,但这会添加 Auto Drafts 的记录,每次更新制作完成。

我也试过在同一文件的 function wp_publish_post() 函数中包含我的插入语句。这没有任何作用。

这些函数中是否有一个特定的部分或者可能是一个完全不同的文件,我应该将我的 INSERT 语句放入其中以在每次发布新的 post 时添加一条记录?

导航到位于 wp-includes 文件夹中的 post.php 文件。

找到 wp_insert_post() 函数并在 return 语句之前使用此代码作为您的 SQL 查询。

if ($postarr['post_status'] == 'publish') {

    try {
        $db = new PDO('mysql:dbname=YOURDBNAME;host=YOURHOST;charset=utf8',
                        'YOURUSERNAME',
                        'YOURPASSWORD');
        $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
        $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
    }
    catch(PDOException $ex) {
        echo "did not connect...";
    }

    $sth = $db->prepare("INSERT INTO YourTable VALUES (?, ?, 0)");
    $sth->execute(array($value1, $value2));

}