XML FOLDER - wp_insert_post - how to assign XML variables to post

I'm trying to repurpose a code that worked for CSV files to do the same for XML- That is import all files in the folder and create Wordpress custom posts(cikkek). So, I managed to parse the XML files in the given folder and get the right variables that I need:

            $posttitle = $xml-THIR-CIM[0];
            $postlead = $xml-THIR-LEAD[0];
            $postcontent = $xml-THIR-HIRSZOVEG[0];

Now my question is, how can I assign these variables to the last part of the code (line 65 - wp_insert_post). I tried simply replacing '$post[title],' with '$posttitle,' but the code does not seem to run.

Here's the whole code:

    ?php
/**
 * Create and insert posts from XML files
 */
add_action( admin_init, function() {
    global $wpdb;

    if ( ! isset( $_GET[insert_mti_posts] ) ) {
        return;
    }

    // CPT SETTINGS
    $mti = array(
        custom-post-type = cikkek
    );

    // Get the data from all those XMLs!
    $posts = function() {
        $data = array();
        $errors = array();

        // Get array of XML files
        $xmlfiles =  glob( __DIR__ . /data/*.xml );

        foreach ( $xmlfiles as $xmlfile ) {

            $xml = simplexml_load_file($xmlfile);
            $posttitle = $xml-THIR-CIM[0];
            $postlead = $xml-THIR-LEAD[0];
            $postcontent = $xml-THIR-HIRSZOVEG[0];

        };

            if ( ! empty( $errors ) ) {
                // ... do stuff with the errors
            }

        return $data;
    };

    // Simple check to see if the current post exists within the
    //  database. This isn't very efficient, but it works.
    $post_exists = function( $title ) use ( $wpdb, $mti ) {

        // Get an array of all posts within our custom post type
    $posts = $wpdb-get_col( SELECT post_title FROM {$wpdb-posts} WHERE post_type = '{$mti[custom-post-type]}' );

        // Check if the passed title exists in array
    return in_array( $title, $posts );
    };

    foreach ( $posts() as $post ) {

        // If the post exists, skip this post and go to the next one
        if ( $post_exists( $post[title] ) ) {
            continue;
        }

        // Insert the post into the database
        $post[id] = wp_insert_post( array(
            post_title = $post[title],
            post_content = $post[content],
            post_type = $mti[custom-post-type],
            post_status = draft
        ));
    }

});

*EDIT: I think the main issue is with $data. It should return an associative array like:

$data = array(
    0 = array(
        title = $posttitle[0],
        content = $postcontent[0],
        description = $postlead[0],


      
    ),
    1 = array(
        title = $posttitle[1],
        content = $postcontent[1],
        description = $postlead[1],
       
    ),
    // ...
);

The issue is I don't know how to pass it this info.

Hope I explained it clearly. Any help is appreciated!

Topic xml wp-insert-post Wordpress

Category Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.