I'm sorry for my english. I'm trying to add some functionalities at my Wordpress website and to do this i have added a function in my function.php theme. The idea is to save in a custom table some datas and create a wp_post for any element added in the custom table.
I hooked the function in this way:
add_action ( 'init', 'inserimentoAutoDatabase' );
The problem is that when the function is executed the query that save the datas in the custom table and create a new post is executed twice. I report my code:
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;
}
}
As you can see in the images below at the end of the cicle (breaked after the first execution) i expect only one element but there are twice.
Anyone can help me??? Have you any idea about this issue?
Thanks in advance.