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

display single post from another multisite WordPress

  • SOLVED

Well, hello!

I have a wordpress multisite setup where the main parent blog stores all of the content as a custom post type. I would like to be able to display any post from the parent blog as a single post in the children, if the user is directed to the correct link

parent - http://gradvantage.com/companies/norton-rose
The page loads correctly as expected

child - http://mediagraduate.com/companies/norton-rose
404 as its not able to access the gradvantage site

Please can you help me with the code to display posts from the parent on child sites

many thanks
nick

Answers (2)

2011-11-08

Luis Abarca answers:

You just need to know the blog and post ID, you can add this data to post meta on child posts


switch_to_blog( $blog_id );

$post = get_post( $post_id );


if you need a custom query from parent blog

$parent_posts = new WP_Query( array('post_type' => 'your-cpt') );

if ( $parent_posts->have_posts() ) :
while ( $parent_posts->have_posts() ) :
the_title();
endwhile;
endif;


npeplow comments:

Thanks Luis

Sadly this isnt working just yet, I added the code above the loop on single.php but its still directing right away to the 404.php

would you be able to take a look directly if I give you login details?

Nick


Luis Abarca comments:

This posts http://mediagraduate.com/companies/norton-rose exists in the child blog ??


npeplow comments:

no, posts are only in the parent blog

by having the url in that format, I would like to it pull the post details from the parent and display it


Luis Abarca comments:

Try to put the code in the file 404.php and when nothing is found in both blogs, then you can show a 404 message


Luis Abarca comments:

404.php

switch_to_blog( $blog_id );

$pagename = get_query_var('pagename'); // this get the 'norton-rose' portion of the url

$search_post = $wpdb->get_results( $wpdb->prepare( "SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_name = %s AND post_type = %s", $pagename, $post_type ));

if ( $search_post)...
... // show the post here
else:
... // put the 404 error msg here
endif;


Where:
$pagename = the slug part in the url
$post_type = your-cpt


Luis Abarca comments:

Im using this code in my 404.php and is working here

[[LINK href="http://dev.justoalblanco.com/companies/pc-now-se-sube-a-la-nube/"]]http://dev.justoalblanco.com/companies/pc-now-se-sube-a-la-nube/[[/LINK]]



switch_to_blog( 1 ); // my main site ID

$pagename = get_query_var('pagename'); // this get the 'norton-rose' portion of the url

$search_post = $wpdb->get_results( $wpdb->prepare( "SELECT ID, post_name FROM $wpdb->posts WHERE post_name = %s AND post_type = %s", $pagename, 'post' ));

if ( is_array($search_post) && sizeof($search_post) > 0 ) {
query_posts('p=' . $search_post[0]->ID );
restore_current_blog();
... // show the post
} else {
// show the 404 error msg
}


npeplow comments:

Thanks, I have included the following code in the 404, but its returning "Auto Draft" , to try and get round this I added AND post_status <> 'auto-draft' but this does not return anything

<?php
switch_to_blog( 1 ); // my main site ID

$pagename = get_query_var('pagename'); // this get the 'norton-rose' portion of the url

$search_post = $wpdb->get_results( $wpdb->prepare( "SELECT ID, post_name FROM $wpdb->posts WHERE post_name = %s AND post_type = %s", $pagename, 'post' ));

if ( is_array($search_post) && sizeof($search_post) > 0 ) {

query_posts('p=' . $search_post[0]->ID );

echo 'in loop';
echo get_the_title($search_post[0]->ID);

restore_current_blog();
} else {
// show the 404 error msg
}
?>


Luis Abarca comments:

Ok, as S├ębastien said, use post_status = publish


$search_post = $wpdb->get_results( $wpdb->prepare( "SELECT ID, post_name FROM $wpdb->posts WHERE post_name = %s AND post_type = %s AND post_status = 'publish' ", $pagename, 'post' ));


Luis Abarca comments:

This is the full version

switch_to_blog( 1 ); // my main site ID

$pagename = get_query_var('pagename'); // this get the 'norton-rose' portion of the url

$search_post = $wpdb->get_results( $wpdb->prepare( "SELECT ID, post_name FROM $wpdb->posts WHERE post_name = %s AND post_type = %s AND post_status = %s", $pagename, 'post', 'publish' ));

if ( is_array($search_post) && sizeof($search_post) > 0 ) {
// get the post
query_posts('p=' . $search_post[0]->ID );

restore_current_blog();

// show the post ....
the_title();
the_content();

} else {
// show the 404 error msg
}


Luis Abarca comments:

It's done amigo, i made some editing in your single.php, check it out.

I have to change pagename to page, because you are using a custom post type and i test it without CPT.


...
$pagename = get_query_var('name'); // this get the 'norton-rose' portion of the url
...

if ( is_array($search_post) && sizeof($search_post) > 0 ) {
$post = get_post($search_post[0]->ID);

restore_current_blog();

setup_postdata($post);

include 'single.php';
} else {
....
}


Luis Abarca comments:

I suggest to add a new file with the content of single.php and include that file in the if, so you can let single.php as default

2011-11-09

S├ębastien | French WordpressDesigner answers:

why not post_status=publish ?