对不起我的英语。 我试图在我的Wordpress网站上添加一些功能,为此我在function.php主题中添加了一个功能。我们的想法是在自定义表中保存一些数据,并为自定义表中添加的任何元素创建一个wp_post。
我以这种方式挂钩了这个功能:
add_action ( 'init', 'inserimentoAutoDatabase' );
问题是当执行该函数时,执行两次在自定义表中保存数据并创建新帖子的查询。我报告了我的代码:
function inserimentoAutoDatabase(){
//get datas from an xml file
$result = fetchData("http://dealer.drivek.it/myPortalXML/index?myPortalXMLkey=d660d1e9-8c1d-41ff-8f54-0829777a9960");
//save the xml
$fp = fopen('autodealerk-1.xml', 'w+');
fwrite($fp, $result);
fclose($fp);
//load xml file
$xml=simplexml_load_file("autodealerk-1.xml");
$i=0;
//start parsing xml content
foreach($xml->car as $auto)
{
//this echo is executed only once
echo ("test");
//not important for the question | checks some content
if($auto->km == null || $auto->km == ''){
$kilometri = "nuova";
$anno_registrazione = "-";
}else{
$kilometri = $auto->km;
$anno_registrazione = $auto->registrationDate;
}
$inevidenza = 0;
if($auto->tractionType){
$inevidenza = 1;
}
// create an array for the query
$insData = array(
'id' => $auto['id'],
'make' => (string) $auto->make,
'model' => (string) $auto->model,
'version' => (string) $auto->version,
'bodyType' => $auto->bodyType,
'fuelType' => $auto->fuelType,
'type' => $auto->type,
'dealer_name' => (string) $auto->dealer->name,
'gear_gearType' => (string) $auto->gear->gearType,
'tractionType' => $auto->tractionType,
'kw' => $auto->kw,
'doors' => $auto->doors,
'seats' => $auto->seats,
'emissionClass' => $auto->emissionClass,
'prices_listPrices' => $auto->prices->listPrice,
'exterior_color_paint' => $auto->exterior->color . " " . $auto->exterior->paint,
'km' => $kilometri,
'typewarrantyMonths' => $auto->warranty->type . " " . $auto->warranty->warrantyMonths,
'equipments' => "equipaggiamenti",
'media' => $auto->image,
'description' => $auto->description,
'registrationDate' => $anno_registrazione
);
$columns = implode("`, `",array_keys($insData));
$escaped_values = array_map('mysql_real_escape_string', array_values($insData));
$values = implode("', '", $escaped_values);
//execute the query ******* the query is executed twice
//i have replaced this line with the following *** read the comment below
//mysql_query('INSERT INTO `auto_importate` (`'.$columns.'`) VALUES (\''.$values.'\')');
$wpdb->insert('auto_importate', $insData);
// define the post
$my_post = array(
'post_title' => (string) $auto->make . (string) $auto->model . (string) $auto->version,
'post_content' => $auto->description,
'post_status' => 'publish',
'post_author' => 1,
'post_type' => "vehicles"
);
// ***** post is created twice
$post_id = wp_insert_post( $my_post );
//i break the cycle **** only for test
$i++;
if( $i == 1 ) break;
}
}
正如您在下面的图片中看到的那样(在第一次执行后断开)我只期望一个元素但是有两次。
任何人都可以帮助我???你对这个问题有任何想法吗?
提前致谢。
答案 0 :(得分:0)
要使用wp_insert_post
阻止重复输入,您可以检查标题的名称是否已存在。如果存在,则可以跳过该插入。
你应该试试这个
if (!get_page_by_title($title, 'OBJECT', 'post') ){
$my_post = array('post_title' => $title,
'post_content' => 'Content',
'tags_input' => $tags,
'post_category' => array(2),
'post_status' => 'publish'
);
$result = wp_insert_post( $my_post );
}