最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

xml - Features in Houzez are not loading in Backend from API

programmeradmin1浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论