简体   繁体   English

WordPress:在多个安装中的CronJob中使用wp_insert_post()

[英]Wordpress: Use wp_insert_post() in CronJob on Multiple installations

I try to insert programmaticly wordpress posts via cron job. 我尝试通过cron作业以编程方式插入wordpress帖子。

I have set up many DB Connections 我已经建立了许多数据库连接

$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')

); );

And want to insert after some DataOperations to insert into corresponding Database, i think i have the connections setup rigth but since wp_insert_post is not part of the class wpdb /global $wpdb it is not working... i couldn´t find a way to select the right database to perform wp_insert_post right. 并想在一些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);
            }
        }
    }
}

can anybody help? 有人可以帮忙吗?

Thanks 谢谢

I would explore using wp-cli (command line scripts for wordpress). 我将探索使用wp-cli (wordpress的命令行脚本)。

You would have a yaml file for each database containing it's own db user and password instead of your $DB array. 每个数据库都有一个yaml文件,其中包含它自己的数据库用户和密码,而不是$ DB数组。

Then write a bash script (or for whatever shell you're using) to loop through each config, change to that directory, and run a wp post create command (the command line equivalent of wp_insert_post, using the same parameters). 然后编写一个bash脚本(或您正在使用的任何shell)循环遍历每个配置,更改到该目录,然后运行wp post create命令(使用相同的参数,等效于wp_insert_post的命令行)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM