How to Pull WordPress Posts into External Website

Est. Reading Time: 5 minutes

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.

define('WP_USE_THEMES', false);

$num = $_GET["numberposts"] ?: '10';
$cat = $_GET["catid"];
$postID = $_GET["postid"];
$type = $_GET["type"];

switch ($type){
   case "singlePost":
      $posts = get_posts('p='.$postID.'&numberposts=1');
   case "catFeed":
      $posts = get_posts('category='.$cat.'&numberposts='.$num.'&order=ASC');
      $posts = get_posts('numberposts='.$num.'&order=ASC');
      <?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(); ?>
            <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 } ?>
      <?php endforeach; ?>

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




XML Structure

   <catname>category name</catname>
   <catdescription>category description</catdescription>
         <id>post id</id>
         <pubDate>December 23, 2008</pubDate>
         <content>full post</content>


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:



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.

Categories: Web Development
Tags: customlistingphp to asppostspullpushwordpressxml feed
Beacon Buddy: