I am trying to figure out how I can have two WordPress installations on same database with different prefixes ( wp_ and wp2_ ) and I need them to interact with each other.

For example I have a Blog on one site and a Directory on another site using same database with two wordpress tables (wp and wp2).

How can i pull the classified listings into the blog site.
like the latest directory listings and etc.

1 Answer 1


Reading through the source code for the wpdb class, I see no reason why something like this shouldn't work:

$otherdb = new wpdb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
$otherdb->prefix = 'wp2_';

And then (if I'm understanding this right) you can use all the existing $wpdb methods and variables. Eg:

$x = $otherdb->get_results( $sql_query );

(If I'm wrong -- or if this is bad practice for reasons I haven't considered -- please feel free to correct me.)

  • Good stuff Pat. You could also just toggle the prefix, but that's not great if you're going to have other developers as $wpdb is usually called globally. Commented Jan 11, 2014 at 2:59
  • 1
    I figured it would be wisest to have a completely different wpdb object, so you could pull data from both sites simultaneously, without confusion.
    – Pat J
    Commented Jan 11, 2014 at 5:56
  • 2
    Be careful when doing this sort of thing. If they're separate installs, then the Users won't be shared between them or might have different IDs, so when you do common things to display posts and such from the other blog, then the ID will be referring to the other users table and you might display incorrect information.
    – Otto
    Commented Jan 11, 2014 at 6:11
  • @Otto: Good point. I was assuming (even though I know I should never assume) that the OP just wanted to pull posts and related content from the wp2_ tables.
    – Pat J
    Commented Jan 11, 2014 at 17:05

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.