dta25920 2014-07-08 10:33
浏览 47

mySQL查询在wordpress网站上执行两次

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.

auto_importate table

auto_importate table content

Anyone can help me??? Have you any idea about this issue?

Thanks in advance.

  • 写回答

1条回答 默认 最新

  • dph58509 2014-07-09 10:35
    关注

    To prevent the duplicate entries using wp_insert_post, you can check the same name of the title already exists or not. If that is exist, then you can skip that insertion.

    You should try this

    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 );
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像