Save custom post type to external database

admin2025-01-07  3

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);
    endif;
}
// Add function to post save hook
add_action('save_post', 'my_data_push');

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);
    endif;
}
// Add function to post save hook
add_action('save_post', 'my_data_push');
Share Improve this question edited Feb 24, 2017 at 14:39 Arinthros asked Feb 24, 2017 at 3:50 ArinthrosArinthros 114 bronze badges 4
  • 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 Commented Feb 24, 2017 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 Commented Feb 24, 2017 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 Commented Feb 24, 2017 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 Commented Feb 24, 2017 at 14:42
Add a comment  | 

2 Answers 2

Reset to default 0

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...

wp_insert_post only works for wordpress own database.

You need to write a custom query using the $wpdb class.

The $wpdb class allows you to interact with multiple databases in WordPress.

global $wpdb;

// Define the database credentials for the other database
$other_db_name = 'other_database_name';
$other_db_user = 'other_database_user';
$other_db_password = 'other_database_password';
$other_db_host = 'other_database_host';

// Connect to the other database
$other_db = new wpdb($other_db_user, $other_db_password, $other_db_name, $other_db_host);

// Define the tables to access
$posts_table = $other_db->prefix . 'posts'; 

// Query the data
$insert = $other_db->insert().

Use $wpdb->insert().

$other_db->insert($posts_table, array(
    'post_title' => 'title',
    'post_name' => 'post-slug', // ... and so on
));
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736259356a583.html

最新回复(0)