I have a custom post type product using ACF (Advanced Custom Fields)

I would like to schedule a daily cron task automation to export all published posts into post_id.txt, one for each post.

I know there are plugins that can export custom post type and ACF to CSV or XML.

But my requirement is a more customized task.

A simplified example, the format of the output file post_id.txt should be and each field separated by \n:

eg. 1234.txt


I can access all these information in the template loop, but how do I write a function to export them as the above job requirement?

Any pointer and direction of where to begin is appreciated.

1 Answer 1


Schedule your backup with WP Cron - https://codex.wordpress.org/Function_Reference/wp_schedule_event

In the cron event, run your query for posts or CPTs and be sure to add 'numberposts' => -1, to return all.

When you loop through your posts from the resulting query, push the values to an array $data[]=$some_string_data_for_prop;

And when you're ready to write the data, convert the array values to a single string; $output = implode("\n", $data);

Then write the data to a file file_put_contents($filepath, $output);

  • All the steps make sense to me. I'm using a W3 Total Cache and I use the system cronjob for my wp-cron execution to ensure it's running at my scheduled time. The thing I'm not sure is where do I put the actual code? In my template file or in functions.php? Or can I create a separate .php file for this task or even making it a dedicated plugin? In this case, how do I use the Wordpress functions if it's a standalone file?
    – KDX
    Aug 8, 2016 at 2:05
  • 1
    It's tricky, you can put it in functions.php but you really only want to turn on/off the event. It's probably best to make it as a plugin (since it's not theme related) and have a button that starts/stops the process.
    – jgraup
    Aug 8, 2016 at 2:08
  • Yes, it's unrelated to theme but I find the theme's loop is the easiest to implment. I understand the logic here, just not sure where do get started. Any pointer to instruction on writing a simple basic plugin?
    – KDX
    Aug 8, 2016 at 2:16
  • 1
    Plugins are pretty easy. It's a file with the same name as the folder in /wp-content/plugins/your-plugin/your-plugin.php - wppb.me - codex.wordpress.org/Writing_a_Plugin
    – jgraup
    Aug 8, 2016 at 3:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.