How to: List recent post on a non-WordPress site

Do you ever wanted to be able to list your WordPress posts on a non-WordPress website? If yes, just read on, I got the solution to do that!

Let's go: Simply paste the following code on your non WordPress site. You may need to upload the wp-config file from the blog you want to get the posts on the same directory.

<?php
$how_many=1; //how many posts to display
require('blog1/wp-config.php'); //the path to the wp-config file of the blog I want to use
$news=$wpdb->get_results("SELECT 'ID','post_title','post_content' FROM $wpdb->posts
 WHERE 'post_type'=\"post\" AND 'post_status'=\"publish\" ORDER BY post_date DESC LIMIT $how_many"); 

foreach($news as $np){
     printf ("<div class='normalText'>%s</div>", $np->post_content);
}?>

That's all. You can also parse rss feeds, but that solution can be simpler.

10 Responses

Dec 05 2008 09:47

Very nice, I use simplepie to do this,

i tried this way but couldn’t get the post to show up. using wordpress.2.6.2.

Dec 08 2008 10:42

This solution can be simpler if your non-wordpress site hosted in one account with Wordpress blog, or MySQL database allowing remote connections to itself, else this script can`t connect to database with Wordpress posts.

Jan 02 2009 06:34

What I’d like to do is list just the first few lines of the latest post on the home page of a non-wordpress site so I can have “Latest from the blog……Blah blah blah blah……Click here to read more”. Etc etc. Any ideas?

Jul 29 2009 02:48

Hi,
I use .tpl (template) files for my html and .php files to push the templates. So where would I place the codes you’ve given?

Sep 01 2009 22:06

Any way to limit this to pull in one category?
Thanks in advance

Sep 10 2009 18:34

Great! The site I would like to use this on currently uses ASP (ugh) but I am planning to switch to PHP at the first of the year. I just might use your script. Definite Bookmark.

Sep 12 2009 08:41

I get this error:
Call to undefined method stdClass::set_prefix() in wp-settings.php on line 268
which is $ prefix = $ wpdb->set_prefix( $ table_prefix ) ; I spaced this line out in case it got erased by the comments. Does this mean I can use the tables prefix being used by the other database and it will work? Is it possible to change the wp_ table prefix after installation without messing up the blog?

Mar 06 2010 23:26

Hmmmm have tried to get this to work using several different methods but no joy.

My wordpress installation is in a directory called ‘blog’

I am trying to load the latest posts onto my sites homepage (which is not part of the WP install)

However – if I include ANY wordpress files (wp-config, wp-load.php etc) then I just get a blank white screen on my homepage??

Anyone any ideas? I am using WP 2.9.2

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required