How to Pull WordPress Posts into External Website

Published January 14, 2013 | Categories: Web Development
I'm sure there are many ways to pull WordPress posts into an external website but here I would like to offer up a simple custom XML solution. No matter what back-end/server-side technology being used (PHP, ASP, ASP.NET, ColdFusion, Ruby, Perl etc.) on the non-WordPress site, most will offer up a way to parse external XML. The problem however is that the out of the box WordPress feed does not include the full post content and limits you to 10 posts. We will need to create a custom feed that will not only provide full post content depending on the ID specified but also allow us to get an unlimited number of posts for our listing page.
Creating WordPress Custom Feed
Copy/paste the following into a new file at the root of your WordPress installation.<?php define('WP_USE_THEMES', false);
require('./wp-blog-header.php');
$num = $_GET["numberposts"] ?: '10';
$cat = $_GET["catid"];
$postID = $_GET["postid"];
$type = $_GET["type"];
switch ($type){
case "singlePost":
$posts = get_posts('p='.$postID.'&numberposts=1');
break;
case "catFeed":
$posts = get_posts('category='.$cat.'&numberposts='.$num.'&order=ASC');
break;
default:
$posts = get_posts('numberposts='.$num.'&order=ASC');
}
?>
<xml>
<items>
<?php if($type == "catFeed"){ ?>
<catname><?php echo get_cat_name($cat); ?></catname>
<catdescription><?php echo category_description($cat); ?></catdescription>
<?php } ?>
<?php foreach ($posts as $post) : start_wp(); ?>
<item>
<id><?php the_id();?></id>
<title><![CDATA[<?php the_title();?>]]></title>
<link><?php the_permalink_rss() ?></link>
<author><?php the_author();?></author>
<pubDate><?php the_date();?></pubDate>
<teaser><![CDATA[<?php the_excerpt();?>]]></teaser>
<?php if($type == "singlePost"){ ?>
<content><![CDATA[<?php the_content();?>]]></content>
<?php } ?>
</item>
<?php endforeach; ?>
</items>
</xml>
As you will see if you go to the page URL the feed will default to all posts and the number that displays defaults to 10. To get exactly what content you want in the custom xml feed we will need to use QueryStrings.
URL QueryString Parameters
- type – Feed type
- singlePost – only pulls post that matches post id parameter
- catFeed – shows a listing of posts from a category
- default – all posts
- postid – post id of the post you’d like to retrieve (only applies to singlePost)
- catid – category id of the category to pull in (only applies to catFeed, optional)
- numberposts – number of posts to display (defaults to 10)
Examples
- http://www.example.com/customFeed.php?catid=244&type=catFeed&numberposts=100 – get 100 of the most recent posts in category with the ID of 244
- http://www.example.com/customFeed.php?postid=13&type=singlePost – get a single post with the id of 13
XML Structure
<xml>
<catname>category name</catname>
<catdescription>category description</catdescription>
<items>
<item>
<id>post id</id>
<title>title</title>
<link>permalink</link>
<author>author</author>
<pubDate>December 23, 2008</pubDate>
<teaser>teaser</teaser>
<content>full post</content>
</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
</items>
</xml>
Back-End/Server Side General Logic
Now that you have the custom feed you can write the back-end code that will parse the custom XML and display blog content on your external website. I would suggest the logic be set up as follows:
- Individual Post Display
- Page that allows for a query string to pull in individual posts
- Display the title, date, author and full post content
- Listing Page Display
- Page that pulls in a category listing (query string is optional)
- Display title, date, author and teaser
- Post titles would link to the individual post page with the specific post id as the query string parameter
Conclusion
If you follow this you can easily pull in your WordPress listings and individual posts via XML and your choice of back-end/server-side scripting language. This opens up a lot of possibilities for highly advanced and customized applications no matter what the platform.
Recent Posts



