Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Query posts from Blog1 to show in Blog2 in a multisite network WordPress

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

Answers (3)

2011-08-24

Maor Barazany answers:

You may use [[LINK href="http://codex.wordpress.org/WPMU_Functions/get_blog_post"]]this function[[/LINK]], 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)


Maor Barazany comments:

You may use [[LINK href="http://codex.wordpress.org/WPMU_Functions/get_blog_post"]]this function[[/LINK]], that will help you get specific post id from specific blog_id in your network


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


Maor Barazany comments:

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


Maor Barazany comments:

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 '/'.


Gary Smith comments:

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


Maor Barazany comments:

I saw you wrote that the <em>get_blog_post</em> 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); ?>


Maor Barazany comments:

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

2011-08-24

Reland Pigte answers:

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


Reland Pigte comments:

or this function :

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


Reland Pigte comments:

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;
?>


Gary Smith comments:

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.


Reland Pigte comments:

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

2011-08-24

Gabriel Reguly answers:

Hi Gary,

Did you know about this plugin [[LINK href="http://wordpress.org/extend/plugins/threewp-broadcast/"]]http://wordpress.org/extend/plugins/threewp-broadcast/[[/LINK]]?

Regards,
Gabriel