I have created a wordpress multisite setup. The main site shows post from all child sites now the problem is i have used switch_to_blog to change the blogs but because of that the admin bar shows link of child site’s dashboard not the main site. The admin bar shows dashboard link of last added post’s site
here is my code:-
if(is_main_site()){
global $wpdb;
global $table_prefix;
// get an array of the table names that our posts will be in
// we do this by first getting all of our blog ids and then forming the name of the
// table and putting it into an array
$rows = $wpdb->get_results( "SELECT blog_id from $wpdb->blogs WHERE
public = '1' AND archived = '0' AND mature = '0' AND spam = '0' AND deleted = '0';" );
//print_r($rows);
if ( $rows ) {
$blogPostTableNames = array();
foreach ( $rows as $row ) :
$blogPostTableNames[$row->blog_id] = $wpdb->get_blog_prefix( $row->blog_id ) . 'postmeta';
endforeach;
//print_r($blogPostTableNames);
// now we need to do a query to get all the posts from all our blogs
// ordered by the number of comments and with limits applied
if ( count( $blogPostTableNames ) > 0 ) {
$query = '';
$i = 0;
foreach ( $blogPostTableNames as $blogId => $tableName ) :
if ( $i > 0 ) :
$query.= ' UNION ';
endif;
$query.= " SELECT post_id,meta_key, $blogId as `blog_id` FROM $tableName WHERE `meta_key` = 'main_site_home_meta_box_check' and `meta_value`='true'";
$i++;
endforeach;
$query.= "ORDER BY post_id DESC LIMIT 5 ";
//echo $query;
$rows = $wpdb->get_results( $query );
//print_r($rows);
// now we need to get each of our posts into an array and return them
if ( $rows ) {
$posts = array();
foreach ( $rows as $row ) :
//print_r($row);
$posts[] = get_blog_post( $row->blog_id, $row->post_id );
switch_to_blog($row->blog_id);
?>
<article id="post-<?php $row->post_id; ?>" <?php post_class(); ?>>
<header class="entry-header">
<h1 class="entry-title"> <a href="<?php echo get_permalink( $row->post_id )?>target='_blank'"><?php echo get_the_title($row->post_id); ?></a></h1>
</header>
<div class="entry-content">
<?php $post_id = $row->post_id ;
$postdata = get_post( $post_id,ARRAY_A ); ?>
<?php print_r($postdata);
echo $postdata['post_excerpt']; ?>
<?php wp_link_pages( array( 'before' => '<div class="page-links">' . __( 'Pages:', 'twentytwelve' ), 'after' => '</div>' ) ); ?>
</div><!-- .entry-content -->
</article><!-- #post -->
<?php
restore_current_blog();
endforeach;
// print_r($posts);
//return $posts;
}
}
I haven’t face such a kind of problem before, According to my view remove your child sites connection from main sites and edit it manually.