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’.
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.
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:
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.wp_posts
table for anything withpost_type=post
andpost_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 tohttp://yoursite.com/xmlrpc.php
that specifies the following parameters: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:
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.