0

I try to insert programmaticly wordpress posts via cron job.

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.

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

Sven Delueg
  • 1,001
  • 11
  • 23

1 Answers1

1

I would explore using wp-cli (command line scripts for wordpress).

You would have a yaml file for each database containing it's own db user and password instead of your $DB array.

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).

manishie
  • 5,302
  • 1
  • 20
  • 21