logo
Ask your WordPress questions! Pay money and get answers fast! (more info)

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.

$23
Query posts from Blog1 to show in Blog2 in a multisite network

I have not been able to quite figure out how to query a specific post / page from 1 blog to show in a theme file for another blog inside a multisite network.

I have tried using the switch_to_blog(1) / return_current_blog() function, but I'm not overly savvy with WP Multisite.

My objective is to gather a function related to wp multisite if possible instead of creating a custom SQL query.

Here is an example I found via the WP Support forums... but I only want to retrieve a specific post or page from a specific blog. I don't need a set of posts, or at least I need to be able to choose from the most recent posts or a specific post.

Thanks

Gary Smith | 08/24/11 at 8:30am Edit
Tutorial: How to assign prize money


(3) Possible Answers Submitted...

See a chronological view of answers?

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

  • avatar
    Last edited:
    08/24/11
    8:39am
    Maor Barazany says:

    You may use this function, that will help you get specific post id from specific blog_id in your network


    <?php get_blog_post($blog_id, $post_id); ?>


    (* It's the same reply, posted twice by mistake)

    Previous versions of this answer: 08/24/11 at 8:39am

    • 08/24/11 8:39am

      Maor Barazany says:

      You may use this function, that will help you get specific post id from specific blog_id in your network


      <?php get_blog_post($blog_id, $post_id); ?>

    • 08/24/11 8:42am

      Maor Barazany says:

      btw: as I was reading lately in the support forums, the switch_to_blog functions is broken in WP3+, so I won't recommend using it.

    • 08/24/11 8:57am

      Maor Barazany says:

      To complete my answer, this way you can use the function for example:


      <?php
      $mypost = get_blog_post( $blog_id, $post_id );
      ?>

      $blog_id - is the id of the blog to pull the post from.
      $post_id - is the id of the specified post in that blog.

      Then you will have an object of the desired post (you may see all data returned in the link in my first reply)
      for example, this will spit out the title and the content:

      <?php
      echo $mypost ->post_title;
      echo $mypost ->post_content;
      ?>

      In this way you can echo all other data that is being retrieved.

      If you do not know the blog id, or want to do this dynamic, you can use the domain of the blog to retrieve the id, with this function, that will return an integer with the id of a blog

      get_blog_id_from_url( $domain, $path = '/' )


      On a subdirectory installation like example.com/blog1/, $domain will be the root 'example.com' and $path the subdirectory '/blog1/'.
      With subdomains like blog1.example.com, $domain is 'blog1.example.com' and $path is '/'.

    • 08/24/11 9:05am

      Gary Smith says:

      Hello Maor,

      I'll see if this works to what I need. Obviously the function that performs the task the most efficiently and / or actually works is going to be the best for me.

      i'll let you know

    • 08/24/11 9:14am

      Maor Barazany says:

      I saw you wrote that the get_blog_post is not working well for you. It should work, notice if you entered the correct blog_id and post_id.
      This is a very small function, you may find it in wp-includes/ms-functions.php
      It just does a query from the db, if the post is not already in the cache.


      function get_blog_post( $blog_id, $post_id ) {
      global $wpdb;

      $key = $blog_id . '-' . $post_id;
      $post = wp_cache_get( $key, 'global-posts' );
      if ( $post == false ) {
      $post = $wpdb->get_row( $wpdb->prepare( 'SELECT * FROM ' . $wpdb->get_blog_prefix( $blog_id ) . 'posts WHERE ID = %d', $post_id ) );
      wp_cache_add( $key, $post, 'global-posts' );
      }

      return $post;
      }



      Maybe you can try to pass casting to integer, something like this (for blog id 5, and post id 8 for example)


      <?php get_blog_post((int)5, (int)8); ?>

    • 08/24/11 9:17am

      Maor Barazany says:

      About the switch_to_blog, it may have been fixed in one of the last versions, anyway it is a more complicated and "expensive" function, you may read in wp-includes/ms-blogs.php starting at line#454

  • avatar
    Last edited:
    08/24/11
    8:42am
    Reland Pigte says:

    You can try this code mate.

    <?php
    switch_to_blog(7);
    query_posts( 'posts_per_page=5' );
    if( have_posts ) while( have_posts() ) : the_post();
    echo '<h1>'.the_title().'</h1>';
    echo '<div>'.the_content().'</div>';
    endwhile;
    restore_current_blog();
    ?>

    Cheers,
    Reland

    Previous versions of this answer: 08/24/11 at 8:42am

    • 08/24/11 8:45am

      Reland Pigte says:

      or this function :

      <?php get_blog_post($blog_id, $post_id); ?>

    • 08/24/11 8:48am

      Reland Pigte says:

      To apply the above code :

      <?php
      $temp = get_blog_post( $blog_id, $post_id );
      echo $temp->ID;
      echo $temp->post_title;
      echo $temp->post_content;
      ?>

    • 08/24/11 9:04am

      Gary Smith says:

      Hello Reland,

      The switch to blog function works, to a degree.... I'm going to test some changes to the query_post portion of the function.

      However, the get_blog_post function doesn't work at least not for anything I tried.

    • 08/24/11 9:09am

      Reland Pigte says:

      Yeah it also works here and I found no trouble on WP3+.

  • avatar
    Last edited:
    08/24/11
    8:42am
    Gabriel Reguly says:

    Hi Gary,

    Did you know about this plugin http://wordpress.org/extend/plugins/threewp-broadcast/?

    Regards,
    Gabriel

This question has expired.



Christianto voted on this question.



Current status of this question: Community pot



Please log in to add additional discourse to this page.





Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.