Title: Loading 2 Blogs with require_once(&#8216;wp-load.php&#8217;)
Last modified: July 12, 2018

---

# Loading 2 Blogs with require_once(‘wp-load.php’)

 *  [dchernik](https://wordpress.org/support/users/dchernik/)
 * (@dchernik)
 * [7 years, 10 months ago](https://wordpress.org/support/topic/loading-2-blogs-with-require_oncewp-load-php/)
 * If I have 2 blogs, xyz.com/a/ and xyz.com/b/, and in xyz.com/index.php I want
   to display 3 latest posts from a and 3 from b, how do I do it?
 * Showing posts from one blog is easy:
 * <?php
    define(‘WP_USE_THEMES’, false); require_once(‘a/wp-load.php’); query_posts(‘
   posts_per_page=3’);
 * while (have_posts()): the_post();
    … ?>
 * After this, how do I switch from blog a to blog b in the same script? Thanks.
 * The page I need help with: _[[log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Floading-2-blogs-with-require_oncewp-load-php%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

Viewing 5 replies - 1 through 5 (of 5 total)

 *  Thread Starter [dchernik](https://wordpress.org/support/users/dchernik/)
 * (@dchernik)
 * [7 years, 10 months ago](https://wordpress.org/support/topic/loading-2-blogs-with-require_oncewp-load-php/#post-10484956)
 * Hmm, this may not be possible, since it requires redefining constants like DB_NAME.
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [7 years, 10 months ago](https://wordpress.org/support/topic/loading-2-blogs-with-require_oncewp-load-php/#post-10485178)
 * Yeah, you can’t invoke two WP installations from one request, that is wrong in
   so many ways beyond redefinition errors 🙂 Of course the recent posts from the
   current site is easy enough. The question is how to get posts from another site,
   same server or not? Probably the simplest is to use the REST API to request the
   posts. The data will be returned in JSON format. Decode the JSON and output the
   desired content.
 * Another way is to instantiate a wpdb class object that connects to the other 
   site’s DB. When you want the other site’s data, swap out the current $wpdb global
   object for your alternate object. Now when you use get_posts() or whatever, the
   data will come from the currently connected, other site’s DB. Once you have the
   desired data, swap the original $wpdb object back in to reconnect to the site’s
   own DB again. There are really two active connections this way, you are just 
   swapping connection references. So saying we are connecting or reconnecting is
   technically incorrect, but makes more conceptual sense than being technically
   accurate.
 *  Thread Starter [dchernik](https://wordpress.org/support/users/dchernik/)
 * (@dchernik)
 * [7 years, 10 months ago](https://wordpress.org/support/topic/loading-2-blogs-with-require_oncewp-load-php/#post-10485706)
 * bcworkz, thanks. I’ve noticed that using
    `$wpdb = new wpdb(...);` works only
   after adding `$wpdb->set_prefix('wp_');` on the next line.
 * Another problem is that functions like `the_permalink()` and `the_excerpt()` 
   still return the old path even after the connection has been swapped.
 * Any ideas?
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [7 years, 10 months ago](https://wordpress.org/support/topic/loading-2-blogs-with-require_oncewp-load-php/#post-10488383)
 * Sounds like a caching issue, probably the WP object cache. Try [wp_cache_flush()](https://developer.wordpress.org/reference/functions/wp_cache_flush/).
 * Good catch with the table prefix. As you may be realizing, I’ve little personal
   experience with this technique, we’re bound to run into details I haven’t considered.
   Nothing insurmountable. Hopefully this covers all of it.
 *  Thread Starter [dchernik](https://wordpress.org/support/users/dchernik/)
 * (@dchernik)
 * [7 years, 10 months ago](https://wordpress.org/support/topic/loading-2-blogs-with-require_oncewp-load-php/#post-10488480)
 * Worked like a charm. Ty.

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Loading 2 Blogs with require_once(‘wp-load.php’)’ is closed to new replies.

## Tags

 * [wp-load](https://wordpress.org/support/topic-tag/wp-load/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 5 replies
 * 2 participants
 * Last reply from: [dchernik](https://wordpress.org/support/users/dchernik/)
 * Last activity: [7 years, 10 months ago](https://wordpress.org/support/topic/loading-2-blogs-with-require_oncewp-load-php/#post-10488480)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
