1

i'm asking here because i have been trying for days to make it work. Basically Im trying to pass XML data from file to Database of WordPress, but i have problem with this piece of XML where i cannot insert into DB, keeping in mind that the XML Tags of Ambients has the same tag name.

XML:

<ad>
<id>
    <![CDATA[427225]]>
</id>
<url>
    <![CDATA[example-mycrm.html]]>
</url>
<title>
    <![CDATA[Rrivadavia  200 - Consulte precio - Casa en Venta]]>
</title>
<type>
    <![CDATA[For Sale]]>
</type>
<content>
    <![CDATA[Example data]]>
</content>
<ambients>
    <ambient>
        <type>
            <![CDATA[Dormitorio con placard]]>
        </type>
        <length>
            <![CDATA[]]>
        </length>
        <width>
            <![CDATA[]]>
        </width>
        <floor>
            <![CDATA[Cerámicos]]>
        </floor>
    </ambient>
</ambients>
<ambients>
    <ambient>
        <type>
            <![CDATA[Dormitorio en suite]]>
        </type>
        <length>
            <![CDATA[]]>
        </length>
        <width>
            <![CDATA[]]>
        </width>
        <floor>
            <![CDATA[Cerámicos]]>
        </floor>
    </ambient>
</ambients>
<ambients>
    <ambient>
        <type>
            <![CDATA[Baño]]>
        </type>
        <length>
            <![CDATA[]]>
        </length>
        <width>
            <![CDATA[]]>
        </width>
        <floor>
            <![CDATA[Cerámicos]]>
        </floor>
    </ambient>
</ambients>
<ambients>
    <ambient>
        <type>
            <![CDATA[Baño en suite]]>
        </type>
        <length>
            <![CDATA[]]>
        </length>
        <width>
            <![CDATA[]]>
        </width>
        <floor>
            <![CDATA[Cerámicos]]>
        </floor>
    </ambient>
</ambients>
<ambients>
    <ambient>
        <type>
            <![CDATA[Living]]>
        </type>
        <length>
            <![CDATA[]]>
        </length>
        <width>
            <![CDATA[]]>
        </width>
        <floor>
            <![CDATA[Cerámicos]]>
        </floor>
    </ambient>
</ambients>
<ambients>
    <ambient>
        <type>
            <![CDATA[Cocina comedor]]>
        </type>
        <length>
            <![CDATA[]]>
        </length>
        <width>
            <![CDATA[]]>
        </width>
        <floor>
            <![CDATA[Cerámicos]]>
        </floor>
    </ambient>
</ambients>
<ambients>
    <ambient>
        <type>
            <![CDATA[Entrepiso]]>
        </type>
        <length>
            <![CDATA[]]>
        </length>
        <width>
            <![CDATA[]]>
        </width>
        <floor>
            <![CDATA[Cerámicos]]>
        </floor>
    </ambient>
</ambients>
<ambients>
    <ambient>
        <type>
            <![CDATA[Lavadero cubierto]]>
        </type>
        <length>
            <![CDATA[]]>
        </length>
        <width>
            <![CDATA[]]>
        </width>
        <floor>
            <![CDATA[Cerámicos]]>
        </floor>
    </ambient>
</ambients>
<ambients>
    <ambient>
        <type>
            <![CDATA[Galería semicubierta]]>
        </type>
        <length>
            <![CDATA[]]>
        </length>
        <width>
            <![CDATA[]]>
        </width>
        <floor>
            <![CDATA[Cerámicos]]>
        </floor>
    </ambient>
</ambients>
<ambients>
    <ambient>
        <type>
            <![CDATA[Galpón]]>
        </type>
        <length>
            <![CDATA[]]>
        </length>
        <width>
            <![CDATA[]]>
        </width>
        <floor>
            <![CDATA[Cerámicos]]>
        </floor>
    </ambient>
</ambients>

Example code:

$file = ("mydomain/myfile.xml");
$xml = simplexml_load_file($file);

$contador = 0;

foreach ($xml as $person) {                 

$Ambients = $person->ambients->ambient->type;  

//Piece of code...
$post_id = wp_insert_post(array(
                'comment_status' => 'closed',
                'ping_status' => 'closed',
                'post_author' => $user_id,
                'post_name' => $slug,
                'post_title' => $Title,
                'post_content' => $Content_title,
                'post_status' => 'publish',
                'post_type' => 'property',
                'guid' => $URL2,
            ));

     //I want each item of Ambients **TYPE** to insert into post data base, but i will only insert one item or in worst cases does not insert nothing in the post
        $queryAmbient = "SELECT * FROM $wpdb->terms WHERE name = '$Ambients' ORDER BY term_id DESC";
        $myAmbient = mysqli_query($conn, $queryAmbient);
        if (mysqli_num_rows($myAmbient)==0){                
            foreach ((array)$Ambients as $Ambient){
                       $ambienttag = sanitize_title($Ambient);
                       $sqlAmbient = $wpdb->query($wpdb->prepare("INSERT INTO $wpdb->terms (name,slug) VALUES ('" . $Ambient ."','" . $ambienttag ."')"));
                       $queryAmbient = "SELECT * FROM $wpdb->terms WHERE name = '$Ambient' ORDER BY term_id DESC";
                       $myAmbient = mysqli_query($conn, $queryAmbient);
                       $ambient_name = mysqli_fetch_assoc($myAmbient);
                       $ambient_id = $ambient_name['term_id'];
                       $sqlAmbientRel = $wpdb->query($wpdb->prepare("INSERT INTO wp_term_taxonomy (term_taxonomy_id, term_id ,taxonomy) VALUES ('" . $ambient_id ."','" . $ambient_id ."','property_feature')"));
            }
        }else{
            foreach ((array)$Ambients as $Ambient){
                       $queryAmbient = "SELECT * FROM $wpdb->terms WHERE name = '$Ambient' ORDER BY term_id DESC";
                       $myAmbient = mysqli_query($conn, $queryAmbient);
                       $ambient_name = mysqli_fetch_assoc($myAmbient);
                       $ambient_id = $ambient_name['term_id'];
            }
        }     
    }

So the idea is to insert all the Ambients Objects tags into the post database.

Here's the Query to insert the ambient_id into post_id of wp_posts:

$sql35 = $wpdb->query($wpdb->prepare("INSERT INTO wp_term_relationships (object_id,term_taxonomy_id,term_order) VALUES ('"  .$post_id.  "','"  .$ambient_id.  "',0)"));
                
$result35 = mysqli_query($conn, $sql35);

Hope you can help me. Thanks in advance.

11
  • What's the problem? What error do you see, what specifically doesn't work? – Rup Oct 27 '20 at 13:54
  • I want each item of <ambients> from XML file to insert into the houzez post, but it only insert one of them. I have no errors in my load at the moment. Its like the foreach statement does not work. – HomerO Oct 27 '20 at 14:00
  • What does your $person look like? I'd guess that's the problem, it only has a single ambients value there? – Rup Oct 27 '20 at 14:06
  • 1
    Why are you using raw SQL queries to manage terms? Is there a reason you're using raw Mysqli functions instead of wpdb? – Tom J Nowell Oct 27 '20 at 14:06
  • 1
    And where is the XML parsing code? – Tom J Nowell Oct 27 '20 at 14: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.