scriptygoddess

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.

Related posts:

  1. Multiple Featured Content Galleries On a site I was working on recently, the client...
  2. Search from WordPress Admin (Post Listing) Redirecting to Home Page This is possibly the most bizarre thing I've ever seen....
  3. Striping IMG tags from the_content in WordPress (and how to fudge page excerpts) Background: For a site I was working on, I was...
  4. Two Blocks of Content for Pages in WordPress I just saw this plugin show up on the WordPress...

Related posts brought to you by Yet Another Related Posts Plugin.

4 Responses to "Pulling in post content from a separate blog install"

1 | whitney

July 27th, 2008 at 2:20 pm

Avatar

thank you SO much for this post. :)

2 | Khaleel

August 7th, 2008 at 1:09 pm

Avatar

@whitney
Agree!

Just snippet tweak

$newpermalink = str_replace("http://www.MYDOMAIN.com","http://BLOG.MYDOMAIN.com",get_permalink()); ?>

Preg_Replace(); surely for this array is not a good way of calling external data.

Thanks though for this post!

3 | Troy

September 7th, 2008 at 6:01 pm

Avatar

Thanks so much works great… only one problem… I am using the following code to show a short excerpt of the post…

Unfortunately my more tag does not use the replacement permalink and points to a non-existent post in the blog under this directory.

How do I make the more tag point to the correct sub blog? You rock by the way!

4 | Ray

March 19th, 2009 at 7:01 pm

Avatar

Hi ScriptyGoddess,

One question, how do we grab widgets from one blog to the other?
Is this possible?

-Ray

Comment Form

Featured Sponsors

About


Advertisements