Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I would like to take the titles of recent posts or content related posts from one website and display them in the widget area of another website. I'm sure there must be a way to do this, perhaps some adaptation to the standard 'recent posts' widget?

I did something similar before using an rss feed from one site and displaying their titles in a module on another. However this was in a non-wordpress site and I am not quite sure how to achieve the same result here.

Ideally I would like to do it without using an rss feed and also be able to control specifically which posts are shown, perhaps being able to define them manually or by other criteria than just 'recent'.

share|improve this question
    
Do you have database access to the other blog? If not, there is no other interface you can use besides RSS/Atom. –  Raphael Nov 11 '10 at 10:20
    
Yes I do have database access –  chentudou Nov 11 '10 at 11:20
    
I'm confused, is the current site you're trying to pull data from a WordPress site? –  prettyboymp Nov 11 '10 at 13:25
    
you could do it very easily with AJAX –  Mild Fuzz Nov 11 '10 at 15:03
    
@prettybomb, yes both sites are wordpress sites. i just wanted to show selected post's titles on site a in the widget area of site b. –  chentudou Nov 11 '10 at 15:33

1 Answer 1

up vote 6 down vote accepted

There are three ways you can accomplish this: two are very code intensive, the other is already built-in.

RSS

Hand's down, the easiest way to do what you want to do is with an RSS widget. WordPress already has an RSS widget built-in to core, so all you need to do is specify the feed and voila!

The widget displays the title by default, but you can also add the post's content, author, and publication date. Tweaking the front-end display to fit your layout is left up to CSS, just like any other widget.

This method doesn't require you to have access to the other site at all and (other than styling the CSS) doesn't require any coding whatsoever.

alt text

Custom Code

If you have direct database access (which you say yo do), you can add a script to the one site that loads content from the other. You can do this one of two ways:

  • Include the WordPress bootstrap file (wp-blog-header.php) and load up WordPress within the second site. Then you can use the standard WordPress query functions to retrieve posts and do with them whatever you want.
  • Use direct database queries to quickly pull information out of the database. You're looking at the wp_posts table for anything with post_type=post and post_status=publish. Just get the title and content, then do whatever you need to do.

I actually used this method on a client site. They had an existing PHP/MySQL-driven home page and wanted to add links to an external WordPress blog. If you go to their site, you'll see a list of blog posts on the front page - the front page is generated by a proprietary CMS that queries the WordPress database to find, parse, and display a list of recent posts.

XML-RPC

WordPress has a fantastic XML Remote Procedure Call system built-in to core. This system allows for external applications (desktop applications, iPhone apps, other websites) to remotely interact with WordPress by sending and receiving XML-formatted messages. There's even an XML-RPC method that does exactly what you want: metaWeblog.getRecentPosts.

So, turn XML-RPC 'on' for the site you want to request posts from. Then send a metaWeblog.getRecentPosts request to http://yoursite.com/xmlrpc.php that specifies the following parameters:

  • ID of the blog you’re working with (usually 0 for a single site)
  • WordPress username
  • WordPress password
  • Number of posts you want to return

WordPress will log you in, run a query to fetch the posts, and return an XML object containing a list of recent posts (as many as you specified) that each contain the following:

  • dateCreated - Post publication date
  • userid – ID of the post author
  • postid – ID of the post itself
  • description – Post content
  • title – Post title
  • link – Post permalink
  • permaLink – Post permalink
  • categories – Array of post categories
  • mt_excerpt – Post excerpt
  • mt_text_more – Read More text
  • mt_allow_comments – Whether comments are open or closed
  • mt_allow_pings – Whether pings are open or closed
  • mt_keywords – Array of post tags
  • wp_slug – Post slug
  • wp_password – Post password
  • wp_author_id – ID of the post author
  • wp_author_display_name – Display name of the post author
  • date_created_gmt – Post publication date (as GMT time)
  • post_status – Post publication status
  • custom_fields – Array of custom fields
  • sticky – Whether or not the post is marked as "sticky"

I wrote a tutorial specific to the MetaWeblog API (which is implemented by WordPress) some time ago. I've also written one that explains how to use the XML-RPC API from within WordPress to make calls to an external WordPress system. That might help to get you started.

If you want to fetch a specific post rather than just "recent" posts, there's a method call for that, too. Just call metaWeblog.getPost and specify the ID of the post you want and your WordPress username and password. This method will return a single post as an XML object containing the same data as I listed above.

share|improve this answer
    
Thanks EAMann. Very complete answer. I will give the 2 latter methods a try, although as my coding skills are weak at best i will probably end up using rss! Thanks –  chentudou Nov 11 '10 at 15:37
    
Why won't you use RSS? It is the most standardized, simple and adaptable solution. –  Raphael Nov 12 '10 at 10:38
    
Read through his question carefully: be able to control specifically which posts are shown, perhaps being able to define them manually or by other criteria than just 'recent'. You can't control which posts show up, which order they show in, or define criteria other than 'recent' if depending solely on RSS. –  EAMann Nov 12 '10 at 15:13
    
Yes, thats mainly why, also I am curous to learn a different way as much as anything. Am getting there slowly! –  chentudou Nov 14 '10 at 16:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.