Ok I'm importing direct from a csv to MySQL, using a mysql_query to insert values. How do you insert category terms into the Wordpress database structure assigning them to a post. Below are two import queries- you will see INSERT INTO wp_terms which I know is not correct, but need help fixing. The rest of the code works beautifully.

    ini_set('memory_limit', '2048M');

    $cn = mysql_connect('localhost','root','1');
    $path = "hotels.csv"; // SET YOUR CSV FILE PATH HERE
    $row = 1;
    if (($handle = fopen($path, "r")) !== FALSE) {
        while (($data = fgetcsv($handle, 100000000, ";")) !== FALSE) {
            $data_entries[] = $data ;
    $final_data = array();
    $count = 0 ;
    foreach ($data_entries as $value) {
        $temp = implode('', $value);
        $tempArr = explode('|', $temp);
        $final_data[] = $tempArr;
        $ins_str = "INSERT INTO wp_posts values('','','','','".mysql_real_escape_string($tempArr[27])."','".mysql_real_escape_string($tempArr[1])."','','".mysql_real_escape_string($tempArr[18])."','','','','".mysql_real_escape_string($tempArr[2])."','','','','','','','','','".mysql_real_escape_string($tempArr[17])."','','')";
        $post_id = mysql_insert_id($cn);

        $ins_str = "INSERT INTO wp_postmeta VALUES('','".$post_id."','list-id','".$tempArr[0]."')";
        $ins_str = "INSERT INTO wp_postmeta VALUES('','".$post_id."','airport_code','".$tempArr[3]."')";
        $ins_str = "INSERT INTO wp_postmeta VALUES('','".$post_id."','location','".$tempArr[4]."')";
        $ins_str = "INSERT INTO wp_terms VALUES('','".$post_id."','job_listing_region','".$tempArr[5]."')";
        $ins_str = "INSERT INTO wp_postmeta VALUES('','".$post_id."','low_rate','".$tempArr[13]."')";
        $ins_str = "INSERT INTO wp_postmeta VALUES('','".$post_id."','high_rate','".$tempArr[14]."')";
        $ins_str = "INSERT INTO wp_postmeta VALUES('','".$post_id."','confidence','".$tempArr[15]."')";
        $ins_str = "INSERT INTO wp_terms VALUES('','".$post_id."','job_listing_category','".$tempArr[16]."')";

    echo "<pre>";

Thanks in advance, Murray

  • What error are you getting or what incorrect data is being inserted? Have you considered using MySQL's CSV engine? – Vérace Jun 7 '14 at 16:52
  • At the moment the data is being inserted as a custom field in wp_postmeta. What I would like is to create category terms under a custom category i.e. job_listing_region. Will check MySQL's CSV engine. – Muzza Jun 7 '14 at 17:08
  • What is the result of printing out $ins_str after the "INSERT INTO wp_terms..." line? I have to tell you, I know virtually nothing about PHP, but the result of the ins_str plus the DDL of wp_terms should start to give us a clue. You haven't said what the error you're getting is! – Vérace Jun 7 '14 at 17:21
  • Have you tried wordpress.stackexchange.com ??? – RolandoMySQLDBA Jun 8 '14 at 12:34
  • After having a good look around I found this post on Wordpress Stack Exchange, though still mind boggled.. will ask questions over there. Thanks :) wordpress.stackexchange.com/questions/139221/… – Muzza Jun 8 '14 at 16:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.