Guest User

Example of pulling posts from another site in a MultiSite install

a guest
Feb 2nd, 2011
3,224
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. global $wpdb; // Globalize the $wpdb object so you can use it
  3.  
  4. /* Set this next variable to whatever the ID is of the sub-site
  5. you want to pull post(s) from */
  6. $sub_blog_id = 2;
  7.  
  8. /* This next line of code will cause most WordPress functions to pull
  9. information from the site with an ID matching $sub_blog_id instead of
  10. pulling it from the current site */
  11. $oldblog = $wpdb->set_blog_id( $sub_blog_id );
  12.  
  13. /* Use the get_posts() function - adding whatever parameters you
  14. desire - to pull information about the post(s) you want to pull
  15. from the sub-site */
  16. $sub_posts = get_posts();
  17.  
  18. /* Loop through the posts you retrieved */
  19. foreach( $sub_posts as $sub_post ) {
  20.   // Do stuff here to output your desired information
  21. }
  22.  
  23. /* Tell the $wpdb object to go back to using the current site */
  24. $wpdb->set_blog_id( $oldblog );
  25. ?>
RAW Paste Data