Home › Forums › General Issues › Populate XML file with ACF custom field data
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 http://www.wpallimport.com/
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.
$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); $doc->save("books.xml");
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.
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We’re hard at work on ACF 6.1, and Beta 1 is now available 🚀— Advanced Custom Fields (@wp_acf) March 16, 2023
This release includes custom post type and taxonomy registration, an improved experience when selecting field types, PHP 8.1 and 8.2 compatibility, and more!
Let’s take a look 🧵https://t.co/Y0WcAT11l4
© 2023 Advanced Custom Fields.