I'm creating an advertising portal for a small network using a custom post type. In order to prevent WordPress database corruption or unauthorized access to user information, I need to keep data separated from the advertising app by duplicating custom post type data to an external database when creating and updating an ad post. I'm using the save_post hook to handle when the function should run, and wp_insert_post() to push all post data into the database. What I want to happen is that the post will save in the WordPress database by default, and also save to an external database using my plugin function.

The issue I can't figure out is how to tell wp_insert_post() to use the external database instead of the global $wpdb variable it uses by default, as outlined in WP code reference.

How can I use a custom database with wp_insert_post? Or is there a better method?

My function, based on WordPress codex and Stack Exchange examples:

// Function to push data on update
function my_data_push ($post_id) {
    // Check for custom post type
    if (get_post_type($post_id) == 'ads'):
        // Set up database connection to external ads storage database
        $push_to_db = new wpdb('username','password','database','host');
        // Insert post data in external database
            wp_insert_post($post_id, $wp_error = true);
// Add function to post save hook
add_action('save_post', 'my_data_push');
  • If it is not in the posts table it is not a CPT. If you need to write to external DB, just write to it however you which. – Mark Kaplun Feb 24 '17 at 4:23
  • Haven't heared yet of any WP solution to connect to another data base. However, while reading your question, Pods framework came to my mind. Pods supports so called Advanced Content Types, which can be stored in separate db tables rather than in wp_posts. Question is, if these tables could in another data base. You may ask their friendly staff in their Slack channel. – Bunjip Feb 24 '17 at 8:04
  • The posts are in the posts table. I'm trying to create a second operation on save that will also save the post data to an external database. Using the wpdb() function, it is possible to connect to another database. The question is how to easily write the complete post data to the external db. – Arinthros Feb 24 '17 at 14:37
  • I updated the post for clarity. What I want to happen are two things: 1. Save the post normally in the WordPress database. 2. Store the same information in an external database. The plugin function above should handle #2. – Arinthros Feb 24 '17 at 14:42

I have been searching for an answer to this question for over a month now - i do not think wordpress is very good for any site that has a secure external DB or for anyone who does not want to store customer data in the main wordpress database, which is strange as that seems like a highly valued function.

At any rate i came across this code during my research that may help you - it is a global variable for a external DB... according to the author it is usable in the same way as $wpdb global variable is used.

If you want the author's full blurb on it go here https://bavotasan.com/2011/access-another-database-in-wordpress/

The code is: $newdb = new wpdb($DB_USER, $DB_PASSWORD, $DB_NAME, $DB_HOST); $newdb->show_errors();

Hopefully it is of some use to you...


For this you need to modify the core wordpress file, in which the function code is written.

You can get the location of the Wordpress functions from the codex ,too.

  • 1
    on this site, updating core files (or even plugin files) is never a valid answer. If it can't be done otherwise, than it just can't be done – Mark Kaplun Feb 24 '17 at 6:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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