Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question

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

share|improve this answer
    
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. –  Andrew Bartel Jan 11 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 Jan 11 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 Jan 11 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 Jan 11 at 17:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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