wp_insert_post from XML feed only inserting first post

You are doing this to add posts to the $data array:

$data = ...

This is not how you add something to an array. You want to append, not assign.

Think of it like writing a list of 5 things, where for each item you throw the list in the trash, get a new piece of paper, and write the item at the top. At the end you will have a list with only the last item.


As an aside, you’ve asked several questions about this code, but have not refactored it, even though it is structured in a very complicated way.

Instead, it would be much easier and faster to rewrite it like this:

files = xml files in folder
foreach file
    xmlfile = load the XML file
    if the post does not exist
        wp_insert_post( [ .... stuff from the XML file ... ] )

Instead of what you have right now:

data = empty array
get xml files function 
    files = xml files in folder
    foreach file
        xmlfile = load the XML file
        add the details to an array
        add that array to data
call the get xml files function and for each thing in the array it returns
    if the post does not exist
        call wp_insert_post with the details