Support » Fixing WordPress » Displaying Latest post from all blogs

  • Hello Everyone,
    First of all I am not very much familiar with WordPress but I do know PHP well. I just stuck at the point where I wanted to display top 4 latest post from all sub blogs of the main blog. I initially thought that whenever someone post something on his/her blog, It would automatically go to main blog page and We can easily grab it from there are display. You can find lot of websites and discussion mentioning this solution and code. But I realized that It is not the way It works. No post from personal blog can appear on main blog page. After much thinking and searching I found a query to get all blog names from the main blog. That’s what I needed to start my coding. At the end I wrote a code to display Top 4 latest post from all blogs. I am providing my code here so in future someone needs to do this can get it easily.

    <?php if(is_front_page()){?>
    
    <?php
    	global $wpdb;
        $query = "SELECT blog_id FROM " . $wpdb->base_prefix . "blogs WHERE spam != '1' AND archived != '1' AND deleted != '1' AND public = '1' AND blog_id != '1' ORDER BY path";
    
        $blogs = $wpdb->get_results($query);
    	//Initializing Array to store post_id,blog_id and post_date
    	$slj_post_id = Array();
    	$slj_post_id[0] = "0";
    	$slj_post_id[1] = "0";
    	$slj_post_id[2] = "0";
    	$slj_post_id[3] = "0";
    
    	$slj_blog_id = Array();
    	$slj_blog_id[0] = "0";
    	$slj_blog_id[1] = "0";
    	$slj_blog_id[2] = "0";
    	$slj_blog_id[3] = "0";
    
    	$slj_post_date = Array();
    	$slj_post_date[0] = strtotime("00/00/0000");
    	$slj_post_date[1] = strtotime("00/00/0000");
    	$slj_post_date[2] = strtotime("00/00/0000");
    	$slj_post_date[3] = strtotime("00/00/0000");
        echo '<ul>';
    	$counter = 0;
        foreach($blogs as $blog){
            $blog_details = get_blog_details($blog->blog_id);
            //echo '<div class="lgLink1"><a href="'. $blog_details->siteurl .'">' . $blog_details->blogname .'</a></div>';
            //echo "BlogID:".$blog->blog_id;
            //echo "<br />";
            $test = get_blog_option($blog_id, 'post_count');
            //echo "Number Of Posts:".$test;  ?>
            <?php switch_to_blog($blog->blog_id); ?>
            <?php //echo "You switched to blog " . $blog->blog_id; ?>
            	<?php $lastposts = get_posts('numberposts=4&orderby=date');
            	foreach($lastposts as $post) :
    				//echo $counter;
    				//echo '<br />';
                    //echo '<span class="lgLink1"><a href="' . get_permalink($post->ID) . '">' . $post->post_title . '</a></span>';
    				//echo '<div style="color:#666666; font-size:11px;">'. mysql2date('j M Y', $post->post_date) .'</div>';
    				//echo '<p>' . strip_tags($post->post_excerpt);
    				//echo '<span class="lgLink1"><a href="' . get_permalink($post->ID) . '">More..</a></span></p>';
    				      //write a code here to get latest 4 post
    					  if($counter < 4 ){
    					  	$slj_post_id[$counter] = $post->ID;
    						$slj_blog_id[$counter] = $blog->blog_id;
    						$slj_post_date[$counter] = strtotime( $post->post_date );
    						$counter = $counter + 1;
    					  }
    					  else
    					  {
    					  	for($j=0;$j<4;$j++)
    						{
    							for($k=0;$k<4;$k++)
    							{
    								if($slj_post_date[$j] > $slj_post_date[$k])
    								{
    									$temp_post_id = $slj_post_id[$j];
    									$temp_blog_id = $slj_blog_id[$j];
    									$temp_post_date = $slj_post_date[$j];
    
    									$slj_post_id[$j] = $slj_post_id[$k];
    									$slj_blog_id[$j] = $slj_blog_id[$k];
    									$slj_post_date[$j] = $slj_post_date[$k];
    
    									$slj_post_id[$k] = $temp_post_id;
    									$slj_blog_id[$k] = $temp_blog_id;
    									$slj_post_date[$k] = $temp_post_date;
    								}//end if
    							}//end for
    						}//end for
    						if(strtotime( $post->post_date )> $slj_post_date[3])
    						{
    							$slj_post_id[4] = $post->ID;
    							$slj_blog_id[4] = $blog->blog_id;
    							$slj_post_date[4] = strtotime( $post->post_date );
    
    							for($j=0;$j<5;$j++)
    							{
    								for($k=0;$k<5;$k++)
    								{
    									if($slj_post_date[$j] > $slj_post_date[$k])
    									{
    										$temp_post_id = $slj_post_id[$j];
    										$temp_blog_id = $slj_blog_id[$j];
    										$temp_post_date = $slj_post_date[$j];
    
    										$slj_post_id[$j] = $slj_post_id[$k];
    										$slj_blog_id[$j] = $slj_blog_id[$k];
    										$slj_post_date[$j] = $slj_post_date[$k];
    
    										$slj_post_id[$k] = $temp_post_id;
    										$slj_blog_id[$k] = $temp_blog_id;
    										$slj_post_date[$k] = $temp_post_date;
    									}//end if
    								}//end for
    							}//end for
    						}
    					  } //end else
    					  //Code finishes here
            	endforeach;
    			?>
    <?php    }
    echo '</ul>';
    	$counter=0;
    	while($counter<4)
    	{
    		switch_to_blog($slj_blog_id[$counter]);
    		$mypost = get_post($slj_post_id[$counter]);
    		//echo "Current Blog ID:".$slj_blog_id[$counter];
    		?>
    		<table>
    			<tr>
    				<td valign="top">
    					<img src="<?php bloginfo('template_directory') ?>/authors/<?php echo $mypost->post_author;?>.jpg" alt="<?php echo $mypost->post_author; ?>" title="<?php echo $mypost->post_author; ?>" />
    				</td>
    				<td>
                    	<?php
    					//get_author_profile($mypost->post_author);
    					//echo '<br />';
    					echo '<span class="lgLink2"><a href="' . get_permalink($mypost->ID) . '" target=_blank>' .get_bloginfo('name'). '</a></span>';		
    
    					echo '<br />';
    					$user_info = get_userdata($mypost->post_author);
    					echo '<span style="color:#000000; font-style:italic; font-weight:bold; font-family:Verdana,Arial,Helvetica,Geneva,sans-serif; font-size:9px; ">'. $user_info->first_name .  " " . $user_info->last_name . '</span>';
    					//echo $mypost->post_content;
    					echo '<div style="color:#666666; font-size:11px;">'. mysql2date('M j, Y', $mypost->post_date) .'</div>';
    					echo '<span class="lgLink1"><a href="' . get_permalink($mypost->ID) . '" target=_blank>' . $mypost->post_title . '</a></span>';
    						echo '<br />';
    					echo '<span style="color:#000000; font-family:Verdana,Arial,Helvetica,Geneva,sans-serif; font-size:11px; ">'.substr($mypost->post_content, 0, 70)."...".'</span>';
    					echo '<br />';
    
    					echo '' . strip_tags($mypost->post_excerpt);
    					echo '<span class="lgLink1"><a href="' . get_permalink($mypost->ID) . '" target=_blank>More</a></span>';?>
                        <br /><br />
    				</td>
    			</tr>
    		</table>
            <?php $counter = $counter + 1;  ?>
    <?php	}
    }
    ?>

    I hope that would be helpful to someone.

Viewing 2 replies - 1 through 2 (of 2 total)
  • This is helpful. Sadly, WP3 breaks this. Do you have it working in WP3?

    Do you have a list of your sub blogs?

    If you do, the easy way to do this would be to use this function below to fetch a list of your blog posts:

    function my_fetch_rss(link='') {
    	$rss = fetch_feed($link);
    	$maxitems = $rss->get_item_quantity(1);
    	$rss_items = $rss->get_items(0, $maxitems);
    	if ($maxitems > 0) {
    		unset($list);
    		foreach ( $rss_items as $item ) {
    			$list .= "<li><a href='".$item->get_permalink()."'>".$item->get_title()."</a></li>";
    		}
    	}
    	return $list;
    }

    For example:

    <h2>thisismyurl.com</h2>
    <ul>
    <?php echo my_fetch_rss('http://www.thisismyurl.com/feed/');?>
    </ul>

    If you want to do the same for multiple you could add a foreach argument to the function and loop multiple blogs.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Displaying Latest post from all blogs’ is closed to new replies.