Wordpress- Query Posts from another wordpress blog on the same server

Discussion in 'WordPress' started by susan8051, May 25, 2009.

  1. #1
    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)
     
    susan8051, May 25, 2009 IP
  2. djuicePK

    djuicePK Peon

    Messages:
    78
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    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
     
    djuicePK, May 25, 2009 IP
    susan8051 likes this.
  3. myp

    myp Well-Known Member

    Messages:
    1,281
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    140
    #3
    myp, May 25, 2009 IP
    susan8051 likes this.
  4. susan8051

    susan8051 Peon

    Messages:
    1,358
    Likes Received:
    49
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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..
     
    susan8051, May 25, 2009 IP
  5. djuicePK

    djuicePK Peon

    Messages:
    78
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    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 :)
     
    djuicePK, May 25, 2009 IP
  6. susan8051

    susan8051 Peon

    Messages:
    1,358
    Likes Received:
    49
    Best Answers:
    0
    Trophy Points:
    0
    #6
    how to unset the options variable.. and how to set it with the new blogs'options.. :)
     
    susan8051, May 26, 2009 IP
  7. djuicePK

    djuicePK Peon

    Messages:
    78
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    
    //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..
     
    djuicePK, May 26, 2009 IP