• 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 to see the link]

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter dchernik

    (@dchernik)

    Hmm, this may not be possible, since it requires redefining constants like DB_NAME.

    Moderator bcworkz

    (@bcworkz)

    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

    (@dchernik)

    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

    (@bcworkz)

    Sounds like a caching issue, probably the WP object cache. Try 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

    (@dchernik)

    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.