【问题标题】:Wordpress: Use wp_insert_post() in CronJob on Multiple installationsWordpress:在多个安装的 CronJob 中使用 wp_insert_post()
【发布时间】:2014-09-20 06:34:26
【问题描述】:

我尝试通过 cron 作业以编程方式插入 wordpress 帖子。

我已经建立了很多数据库连接

$DB = array(
'naruto' => new wpdb('ultimate_naruto','','ultimate_naruto','localhost'),
'4k' => new wpdb('ultimate_4k','','ultimate_4k','localhost'),
'weihnachten' => new wpdb('ultimate_xmas','','ultimate_xmas','localhost'),
'halloween' => new wpdb('ultimate_hallow','','ultimate_hallow','localhost'),
'disney' => new wpdb('ultimate_disney','','ultimate_disney','localhost'),
'starwars' => new wpdb('ultimate_starwar','','ultimate_starwars','localhost'),
'fossil' => new wpdb('ultimate_fossil','','ultimate_fossil','localhost'),
'avocado' => new wpdb('ultimate_avocado','','ultimate_avocado','localhost'),
'3d-drucker' => new wpdb('ultimate_3d','','ultimate_3d','localhost'),
'lego' => new wpdb('ultimate_lego','','ultimate_lego','localhost'),
'barbie' => new wpdb('ultimate_barbie','','ultimate_barbie','localhost')

);

并且想在一些 DataOperations 之后插入以插入相应的数据库,我想我的连接设置是正确的,但是由于 wp_insert_post 不是 wpdb /global $wpdb 类的一部分,所以它不工作......我找不到一种选择正确的数据库来执行 wp_insert_post 的方法。

foreach($arr_keywords as $portal => $keywords){
    foreach($keywords as $keyword){
        if(stripos($_title,$keyword) !== false){
            $row = $DB[$portal]->get_results("SELECT COUNT(*) FROM wp_posts WHERE id='$id'");
            if($row == 0){
                $data = prepare_data($id,$arr_portal[$portal]);
                //do something to select correct db here
                wp_insert_post($data);
            }
        }
    }
}

有人可以帮忙吗?

谢谢

【问题讨论】:

  • 我可以只使用 SQL 来管理它,但是帖子没有正确创建 -> 没有永久链接等

标签: php wordpress cron multiple-databases


【解决方案1】:

我会探索使用wp-cli(wordpress 的命令行脚本)。

每个数据库都有一个 yaml 文件,其中包含它自己的数据库用户和密码,而不是您的 $DB 数组。

然后编写一个 bash 脚本(或您使用的任何 shell)来循环遍历每个配置,切换到该目录,然后运行 ​​wp post create 命令(命令行等效于 wp_insert_post,使用相同的参数)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-22
    • 1970-01-01
    • 2019-03-23
    • 1970-01-01
    相关资源
    最近更新 更多