它肯定可以以更有效的方式编写(未经测试):
$added = array();
global $wpdb;
foreach($uniques as $unique){
$pagetitle = getTitle($unique);
$new_post = array(
\'post_title\' => $unique,
\'post_status\' => \'publish\',
\'post_type\' => \'websites\'
);
$pid = wp_insert_post($new_post);
if ($pid) {
$wpdb->query( $wpdb->prepare(
"INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value) VALUES
(%d, \'%s\', \'%s\'), (%d, \'%s\', \'%s\')",
$pid, \'title\', $pagetitle, $pid, \'url\', $unique
) );
$added[] = $pid;
}
}
if ( ! empty($added) ) {
$ids = implode(\',\', $added);
$wpdb->query("UPDATE $wpdb->posts SET `post_title` = `ID`, `post_name` = `ID` WHERE `ID` IN ($ids)");
}
对于600个URL,此代码运行的查询数比您的少1200个,但可能还不够。。。