What I've done so far is create a plugin that creates a custom post type, takes a field(ID from the custom DB) and also creates a couple of page templates.

I have a custom MySQL DB that is on the same server as a WP install. What I need is to import a MySQL DB table into a WP custom post type.

I can't figure out how to import the data itself straight from one MySQL DB to the WP using PHP. The catch is, I need this to happen whenever the custom MySQL DB is updated.

If it helps, we do have a REST API that we can use. I was just trying to make this as hands off as possible.

Any and all help is appreciated!!

  • DB events are not intrinsically monitored by PHP. I would suggest you to use MySQL trigger dev.mysql.com/doc/refman/8.0/en/trigger-syntax.html to update one DB to another DB. You can instruct any SQL statement to execute as event triggered. If you rely on PHP solution, it will be cumbersome to implement. Commented Feb 23 at 7:53

2 Answers 2


WordPress Posts are not stored in a single table. Postmeta is stored elsewhere, and of course each table has its own format. If you ever publish content directly in WordPress (posts, pages, attachments, etc.) you could quickly run into issues with post IDs. Even if not, it would potentially require some fairly significant data manipulation to convert your non-WP data to WP-useable structure.

In this case, I'd say a REST API would be a much better way to go, and it would also be more forward-compatible.

  • I understand about the WP DB and how it's constructed. I'm just not sure how I can update that programatically from another DB with all my info.
    – lz430
    Commented Feb 20, 2017 at 21:41

I'm not sure how helpful this might be; but I've done similar things when migrating platforms; or performing synchronizations between WooCommerce and Point-of-Sale systems.

I'd suggest making a PHP script (that would sit inside the plugins folder, in WordPress - eg /wp-content/plugins/my-importer/import/import.php), that hooks into the native WordPress framework, but doesn't load the theme (this allows you to use wp_insert_post(), wp_update_post(), update_post_meta(), etc; without loading unwanted output).

Set up the script so it touches the REST API, asking for any updates; collects the required information; then uses the native WordPress functions to add and update information as required.

You could potentially use wget via a cron, to trigger the import regularly.

I know that's a bit general; but it might point you in the right direction.


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.