Domain flipping by Millionaire Society

Want to earn a regular income from domain flipping? You should watch this VIDEO!

Loading Posts From Two WordPress Databases

Posted on April 18th, 2010 by admin in Domain Development, Domain News | 5 Comments »

I swear by WordPress. It is my CMS of choice. I have posted more then once about it’s usability for both developing blogs and main-stream websites.

Here is a problem/scenario I ran into while developing a feature for a WordPress site I run that I had a lot of trouble finding a solution for – so much so that I think it’s worth posting about.

A website I run runs two WordPress instances – one for the core site and one for the site’s blog. I had to split the site into two WordPress instances because the core site utilizes the post ids in a special way and I can’t have blog posts interfering with that – but that’s a story for another time.

Anyways, I wanted to show the blog posts on the core site – and I couldn’t simply read the blog’s RSS feed since I had to make use of a lot of custom-field values which don’t come along with a RSS feed entry. The custom fields are the reason why I couldn’t write my own sql query as well since I wanted to use the WP_Query class and all the post-handling functionality that goes with it.

So, my requirement was to read and display posts on one WordPress website from another WordPress database ie read data from two WordPress databases using the global $wpdb and the inbuilt WP_Query class with all its bells and whistles.

Some people have shown how to connect to and get stuff from a WordPress database externally, others have shown how to install two WordPress blogs for one site, but I have not found one resource that clearly describes and provides a solution for my predicament.

Believe it or not, it is not as simple as reassigning the global $wpdb object. That does not work.

So, here is how to connect to two (or more) WordPress databases from the one WordPress instance and use all the cool post-handling functionality that wpdb and the WP_Query classes provide:

1. Let’s assume the first database is called db1 and the second one is db2.

2. Set your first datbase’s table prefix to prefix1_ and your second database’s table prefix to prefix2_ (you can use this cool plugin from SEOEggHead to easily change your database’ table prefixes).

3. Install the hyperdb plugin – the database performance enhancing plugin used by WordPress.com and other high traffic WordPress sites.

4. Configure the hyperdb db-settings.php file to read from the 2 database servers by adding the two databases via add_db_server();
add_db_server(‘global’, 0, ”, 1, 1, DB_HOST, DB_HOST, DB_NAME, DB_USER, DB_PASSWORD);
add_db_server(‘db2′, 0, ”, 1, 1, DB_HOST, DB_HOST, ‘db2′, DB_USER, DB_PASSWORD);

5. Also inside db-settings.php, tell hyperdb to query the second database for all tables belonging to that database using a call to add_db_table (flag required tables via the specified method call like so):
add_db_table(‘db2′, ‘prefix2_commentmeta’);
add_db_table(‘db2′, ‘prefix2_comments’);
add_db_table(‘db2′, ‘prefix2_links’);
add_db_table(‘db2′, ‘prefix2_options’);
add_db_table(‘db2′, ‘prefix2_postmeta’);
add_db_table(‘db2′, ‘prefix2_posts’);
add_db_table(‘db2′, ‘prefix2_terms’);
add_db_table(‘db2′, ‘prefix2_term_relationships’);
add_db_table(‘db2′, ‘prefix2_term_taxonomy’);
add_db_table(‘db2′, ‘prefix2_usermeta’);
add_db_table(‘db2′, ‘prefix2_users’);

6. Finally, once you have your multiple database connections setup, to read posts from the second WordPress database inside your first WordPress instance’s code, do the following:

global $wpdb;
$wpdb->set_prefix(‘prefix2_’);

Read your second database’s posts and display or do other operations with them. Once done, don’t forget to change $wpdb’s prefix back to the original:

$wpdb->set_prefix(‘prefix1_’);

Works like a charm!

By the way, the HyperDB plugin is used by WordPress.com and other high traffic WordPress sites to help reduce the load on their database servers. It is an awesome plugin that lets you create fallback servers, user master/slave setups for reading/writing data to different databases, use completely separate servers for different tables in your WordPress installation and many other useful things that can help you administer a high traffic WordPress site.

Want To Learn More About Flipping Domains For Profit: Read My Post On Domain Flipping!

5 Responses to Loading Posts From Two WordPress Databases

  1. I love WordPress although lately I have encountered a major issue with the WP core that significantly decreases the speed of pages loading when you have more than a couple hundred pages (as opposed to posts). This is really poor form for WP as they are starting to market themselves as a CMS rather than just blog software. Here is some info: http://wordpress.org/support/topic/285382 (I am “JoelJonathan” in the thread) – any ideas?

  2. admin says:

    Interesting Joel.

    I haven’t had the need to use the static pages in such high numbers for anything – as I have always managed to get the posts to cater to all my needs.

    Is there a particular reason you couldn’t use posts instead of pages? Pages are basically posts with just a special kind of permalink – but I find that I can mimic any kind of permalink structure when I need to with posts.

    John

  3. The biggest advantage to working with pages is the built in hierarchical structure because of the parent/child pages with unlimited nesting and then the nice and clean way to use page templates – you are right though in that you can certainly do both of those things using posts but it just takes some hacking and isn’t as nice and clean as pages.

    Also, we have found from personal experience that even when using posts for a very large site (500k+ posts) the load times slows way down – granted, we didn’t use the hyperDB plugin you mentioned above and maybe there were some other things we could have done but do you have any experience/advice with using WP for very large sites (500k+ posts)?

  4. Pingback: HyperDB And Strange Characters | Domain names, news and opinions

  5. Vijay says:

    Its giving the following error…

    Fatal error: Call to undefined function add_db_server() in D:\wamp\www\softaula\db-config.php on line 315

Leave a Reply

Your email address will not be published. Required fields are marked *

*

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Explore other similar posts that might interest you

Click on a post title to read the post!