03 Jun, 2008
Pulling in post content from a separate blog install
Posted by: Jennifer In: WordPress|WordPress Hacks
I had a site I was using WordPress for – and it actually involved two separate installs. One install was set up as the main content management for the site. Pages were obviously the pages of the site. Posts were for news and Press Release articles. A separate install was for the "corporate blog."
I had one page where I listed the news and PR items, and I was asked to also list the latest blog posts on that page as well. Since the blog was actually an entirely different install, this presented the problem. The critical issue here is that both WordPress installs are in the same database – just with different prefixes. This makes the problem pretty easy to deal with.
So just to go over the other aspect of the page – listing the news and Press Releases – that is simple. I get the id of the categories (lets say that news has an id of '5' and PR has and id of '6') on the page template I create especially for that page I have the following:
<h3>News</h3>
<?php
global $post;
//category=5 - this is the id for news
$myposts = get_posts('numberposts=5&category=5');
foreach($myposts as $post) :
?>
<p><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><br />
<em><?php the_time('F jS, Y') ?></em></p>
<?php endforeach; ?>
<h3>Press Releases</h3>
<?php
global $post;
//category=6 - this is the id for Press Releases
$myposts = get_posts('numberposts=5&category=6');
foreach($myposts as $post) :
?>
<p><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><br />
<em><?php the_time('F jS, Y') ?></em></p>
<?php endforeach; ?>
Now to pull in the post content from the other blog – you need to a swap in the prefix you're using for the other install (where I have "BLOGPREFIX" below). Also had to manipulate the URL given by "get_permalink()". See my note below in the code.
<h3>Blog Posts</h3>
<?php
$querystr = "SELECT * FROM BLOGPREFIX_posts WHERE post_status = 'publish' AND post_type = 'post' ORDER BY post_date DESC limit 5";
$blogposts = $wpdb->get_results($querystr, OBJECT);
if ($blogposts): ?>
<?php foreach ($blogposts as $post): ?>
<?php setup_postdata($post); ?>
<?php /*
Normally you use the_permalink() to display the URL -
but that would show the post as if it was part of the
current blog. In my case, the blog was actually set up
as a subdomain. There may be a better way to do this but
I just stripped out the domain and replaced it with the
blog's subdomain. See below */
$newpermalink = str_replace("http://www.MYDOMAIN.com","http://BLOG.MYDOMAIN.com",get_permalink()); ?>
<p><a href="<?php echo $newpermalink; ?>"><?php the_title(); ?></a><br />
<em><?php the_time('F jS, Y') ?></em></p>
<?php endforeach; ?>
<?php else : ?>
<p> Not Found</p>
<?php endif; ?>
That will pull the last 5 blog entries – with the title linked to the post and the date below it.