I have multiple wordpress databases on the same server. I am not using wordpress MU. I have multiple wordpress installations on the same server. Is it possible to query posts from blog1 so that i can display an excerpt of some posts of blog1 on blog2's sidebar.. Actually i want to display posts tagged with particular tag.. (Otherwise i would have done it using some SQL select operations)
probably an easy solution would be like this <?php function get_other_posts(){ $wpdb_old = wp_clone($GLOBALS['wpdb']); $wpdb_new = &$GLOBALS['wpdb']; // All you have to care about following two lines $wpdb_new = new wpdb('your_db_username','db_password','new_db_name','localhost'); $wpdb_new->set_prefix('wp_'); //Now whatever function you'll call, it will use new database $posts = get_posts('numberposts=5'); //We are done so lets take the old wpdb back on its position $wpdb_new = $wpdb_old; } ?> Code (markup): Code is commented as well.. let me know if any problem
thanks a bunch buddy.. this code is amazing.. but the functions like the_permalink(); uses the current blog's urls.. Guess we need to change some other variables too.. +Reps added..
Then try using WP_Query instead of get_posts, this will let you to make your own loop and so your own the_permalink.. because we already change the context of wpdb globally so whatever function wordpress will call it will be from new database.. even for the get_option function.. or try to unset the options variable which is usually saved in global cache or memory cache (if you enabled wp-cache) Thanks for the reps
//use it after you setup new wpdb unset($GLOBALS['alloptions']); get_alloptions(); Code (markup): I won't recommend it, you should use WP_Query way instead of get_posts... this way you can use your own variable's the_permalink, the_title etc etc..