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:

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

    if ( ! isset( $_GET["insert_mti_posts"] ) ) {

    $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"] ) ) {

        // 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!

  • 1
    Your code is very difficult to read, can you fix the indenting? – Tom J Nowell Feb 12 at 17:10
  • Sure, better now? Thanks for having a look! – Pbalazs89 Feb 12 at 17:18
  • A little but not much, most of the code still has has incorrect indentation, there are functions with contents that aren't indented at all. Anywhere inside a curly bracket needs to be indented one level deeper than its surroundings – Tom J Nowell Feb 12 at 21:49
  • Had another go at it, and simplified the code as well. – Pbalazs89 Feb 13 at 8:11
  • Are there sections missing? e.g. the $posts function has a data array, but you never add anything to it, it's just an empty array. Also the $post_exists is missing a level of indenting, which editor are you using to code, your editor should do all of this for you automatically – Tom J Nowell Feb 13 at 10:46

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.