How to use multiple database in wordpress?

I want to fetch some post from another wordpress site which is hosted in the same server. How can I fetch those post?

Is multiple database connection is possible in wordpress?

Related posts

Leave a Reply

3 comments

  1. It is possible but you must manually create a new instance of the wpdb class with right settings for your other database.

    Quote from the wpdb Codex page:

    The $wpdb object can talk to any number of tables, but only one
    database: the WordPress database. In the rare case you need to connect
    to another database, you will have to instantiate your own object from
    the wpdb class with the appropriate connection details. For extremely
    complicated setups with many databases, consider using hyperdb
    instead.

  2. 1) Download class db at link: https://codeshack.io/super-fast-php-mysql-database-class/
    – rename class db = db_custom

    2) …/themes/theme-name/functions.php

    $dbhost = ‘db_ip’; // 127.0.0.1
    $dbuser = ‘db_user’; // input database username
    $dbpass = ‘db_user_password’; // input your database password
    $dbname = ‘db_user_name’; // input your datatabase name

    $db_watch = new db_custom($dbhost, $dbuser, $dbpass, $dbname);
    $sql = “SELECT * FROM products LIMIT 0,2”;
    $results = $db_watch->query($sql)->fetchAll();
    print_r($results);

    I have tested my website WordPress it’s working well