我有一个项目,我在其中创建了一个包含20多万行的数组,其中包含25个字段,这些字段运行wp\\u insert\\u post和update\\u post\\u meta。要开始它的处理速度相当快,在10公里左右的记录之后,速度会慢很多。
有没有更有效的方法?是否存在MySQL存储过程,以便我可以直接使用SQL查询创建这些帖子并跳过PHP?
echo "get list of posts to be created in wordpress \\n";
//RETURNS ALL OFFER DATA OF POSTS THAT NEED TO BE ADDED
$sql = "SELECT a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z FROM offer WHERE modified = \'1\';";
$rst = $wpdb->get_results($sql,ARRAY_N); //run query
if(is_null($rst)){
echo mysql_error() . "\\n";
}else{
echo "done \\n";
}
echo "creating posts \\n";;
foreach($rst as $r){
//print_r($r);
$my_post = array(
\'post_title\' => $r[0],
\'post_status\' => \'publish\',
\'post_author\' => 72,
\'post_category\' => array(16)
);
// Insert the post into the database
$newOffer = wp_insert_post( $my_post );
if(!$newOffer){
echo "problem creating the post " . $r[0];
}
array_unshift($r,$newOffer); // so that the data positions matches when calling updateMetaData
if($newOffer){
updateMetaData($newOffer,$r);
}
}
function updateMetaData($id,$data){
global $wpdb;
$category = $wpdb->get_results(\'SELECT categoryName FROM category WHERE categoryIdentifier ="\'.mysql_escape_string($data[8]).\'"\');
if($category){
wp_set_object_terms( $id, array($category[0]->categoryName,16), \'category\');
}
update_post_meta($id,\'a\',\'yes\');
update_post_meta($id,\'b\',$data[1]);
$year = substr($data[2],0, -4);
$mon = substr($data[2], -4, 2);
$day = substr($data[2], -2);
update_post_meta($id,\'c\',$mon."/".$day."/".$year." 12:00:00 AM");
$year = substr($data[3],0, -4);
$mon = substr($data[3], -4, 2);
$day = substr($data[3], -2);
update_post_meta($id,\'d\',$mon."/".$day."/".$year." 12:00:00 AM");
update_post_meta($id,\'e\',$data[4]);//description
update_post_meta($id,\'f\',$data[5]);
update_post_meta($id,\'g\',$data[6]);
update_post_meta($id,\'h\',$data[7]);
update_post_meta($id,\'i\',$data[8]);
update_post_meta($id,\'j\',$data[9]);
update_post_meta($id,\'k\',$data[10]);
update_post_meta($id,\'l\',$data[11]);
update_post_meta($id,\'m\',$data[12]);
update_post_meta($id,\'n\',$data[13]);
update_post_meta($id,\'o\',$data[14]);
update_post_meta($id,\'p\',$data[15]);
update_post_meta($id,\'q\',$data[16]);
update_post_meta($id,\'r\',$data[17]);
update_post_meta($id,\'s\',$data[18]);
update_post_meta($id,\'t\',$data[19]);
update_post_meta($id,\'u\',$data[20]);
update_post_meta($id,\'v\',$data[21]);
update_post_meta($id,\'w\',$data[22]);
update_post_meta($id,\'x\',$data[23]);
update_post_meta($id,\'y\',$data[24]);
update_post_meta($id,\'z\',$data[25]);
$year = null;
$mon = null;
$day = null;
//echo "wordpress post meta updated.\\n";
}