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.
]]>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.
]]>$wpdb = new wpdb(...);
$wpdb->set_prefix('wp_');
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?
]]>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.
]]>