Home Forums General Issues Populate XML file with ACF custom field data


Populate XML file with ACF custom field data

  • Hello,

    I have a custom post type that has 10 ACF custom fields within.
    I would like to know the following.

    1) How to i generate an XML file with the ADC field data from posts in that post type?

    2) How do i populate a ACF field using that XML data on the fly.

    The reason i ask is i would like to have a site that i create posts and have that post info populate an XML file and that file is then used to populate another websites post. Both websites will have the exact same fields and field names, but probably different keys.

    Why this is important is that i can populate multiple websites from a single point, “admin website”.

  • Exporting XML should be fairly straightforward if you’re comfortable writing PHP. You can create a page template for your custom post type, (like single-custom.php,) and omit all of the HTML output from that template (such as the get_header/get_footer calls). Write your PHP to output XML instead; possibly using a PHP library and HTTP header. There are plenty of tutorials on writing XML with PHP available by search.

  • The major problem that you have is that the fields will have the same names but not the same field keys. Otherwise you use the standard WP export and import. This will be a problem exporting an importing with almost any tool. You’d need to look up the fields to get the field keys to insert.

    WP All Import is not a free plugin, but the pro version has an addon that will import into ACF fields. That’s probably the path I’d take unless I had a lot of time to kill that I could spend building something to do it myself

  • Thanks Hazard that was closest to the solution.
    I created an archive-custom-pt.php and used $title= get_field('title'); to populate the XML document.

    Sample idea

    $doc = new DOMDocument('1.0','UTF-8');
    $doc->formatOutput = true; 
    $doc->preserveWhiteSpace = false;
    $root = $doc->createElement('Book');
    $root = $doc->appendChild($root);
    // Title
    $title = $doc->createElement('title');
    $title = $root->appendChild($title);

    The only issue i have now is it will write the post custom field data to the books.xml but only a single post and not all the posts.

    Thank you for your help!

  • I’d assumed you’d output XML directly on the page request, but sending it to a separate file works too.

    If WordPress’ settings aren’t getting you the output you need on the archive page, you could do a call to wp_query before your XML output.

    Specifically, I think you want to set ‘posts_per_page’ to -1 to override the pagination.

  • The output to the actual archive page works perfectly, i can see all the posts; however when i create the new Domdocument and save it (all within the wp loop) it saves the page (name.xml) but it only shows a single post and not all the posts in that post type loop.

Viewing 6 posts - 1 through 6 (of 6 total)

The topic ‘Populate XML file with ACF custom field data’ is closed to new replies.