Hmm, this may not be possible, since it requires redefining constants like DB_NAME.
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.
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?
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.