I recently got a request from a client of mine to setup two different wordpress blogs for her company and then display the posts from both blogs in a single list on the home page of her web site. I wasn’t able to find much documentation of people doing this type of thing through various searches, so I figured I’d post an entry on it when I finished. As a disclaimer, I’m sure that this can be done more efficiently or in less code. If you have a better way to do it, please leave me a comment. I’m always open to learning new techniques.
The first step is to set up two includes that will make the connection to your database. In my case, I called these “blog_db_connect.php” and “blog_db_connect2.php”. Here’s an example of how they should look:
$hostname=your hostname;
$username=your username;
$password=your password;
$dbname=your dbname;
$dbh = mysql_connect($hostname, $username, $password);
mysql_select_db($dbname);
You would create one of these each with the credentials of each of your blog databases.
Next, you’ll need to write some php code that connects to each blog database and sucks in the posts. The query I used for each blog to get only the five most recent published posts is below. This will get me the post id, post title and date that the post was published. (It’s important to search for posts with a post_status of ‘publish’, otherwise you’ll get all the drafts that go with it.)
select `ID`, `post_title`, `post_date` from `wp_posts` where `post_status` = ‘publish’ order by `post_date` desc limit 0,5
Once you have the posts, you’ll want to iterate over them and store the fields you returned into an array. A few notes about the code below: 1) I’m setting various things specific about this blog such as the blog’s title and the url where the blog sits. These are defined in the $blog1title and $blog1url variables. 2) The post information is being stored in an associative array with the post’s date being the key. This allows me to do a sort based on the array’s key (krsort()) later on and get my posts in chronological order.
$result = mysql_query($blogQuery);
while($row=mysql_fetch_array($result)) {
$post = array();
$post['title'] = $row["post_title"];
$post['source'] = $blog1title;
$post['blog_url'] = $blog1url;
$post['url'] = $blog1url.’/?p=’.$row['ID'];
$post['date'] = date("m/d/y", strtotime($row["post_date"]));
$posts[strtotime($row["post_date"])] = $post;
}
Once you’ve stored all of your posts in the array, you’ll want to do a ksort:
krsort($posts)
Here’s the full code for building and sorting the array for both blogs:
$posts = array();
/** Blog 1 **/
include(‘includes/blog_db_connect.php’);
$blogQuery = "select ID, `post_title`, `post_date` from `wp_posts` where `post_status` = ‘publish’ "
." order by `post_date` desc limit 0,5";$blog1url = "http://www.blog1.com";
$blog1title = “Blog 1″;$result = mysql_query($blogQuery);
while($row=mysql_fetch_array($result)) {
$post = array();
$post['title'] = $row["post_title"];
$post['source'] = $blog1title;
$post['blog_url'] = $blog1url;
$post['url'] = $blog1url.’/?p=’.$row['ID'];
$post['date'] = date("m/d/y", strtotime($row["post_date"]));
$posts[strtotime($row["post_date"])] = $post;
}/** Blog 2 **/
include(‘includes/blog_db_connect2.php’);$blog2url = "http://www.blog2.com";
$blog2title = "Blog 2";$result = mysql_query($blogQuery);
while($row=mysql_fetch_array($result)) {
$post = array();
$post['title'] = $row["post_title"];
$post['source'] = $blog2title;
$post['blog_url'] = $blog2url;
$post['url'] = $blog2url.’/?p=’.$row['ID'];
$post['date'] = date("m/d/y", strtotime($row["post_date"]));
$posts[strtotime($row["post_date"])] = $post;
}
krsort($posts);
Finally, you want to iterate over the post entries and display them on your page. Here’s a simple example of what I did to display them in an unordered list:
<ul>
<? foreach($posts as $post) {?>
<li>
<a href="<?=$post['url']?>"><?=cleanString($post['title'])?></a>
<ul class="blog-title">
<li><a href="<?=$post['blog_url']?>"><?=$post['source']?></a> – <?=$post['date']?></li>
</ul>
</li>
<? }?>
</ul>
The clean string is a function I’m using to cleanse the blog title of any invalid content. You can see more about that function here.