Get WordPress posts out of WordPress without using inbuilt functions

Posted By: KP On December 26, 2010 Under Other, Programming, Wordpress Categories

When wordpress installation is not on the same host inbuilt wordpress function get_posts and require(‘wp-blog-header.php’); will not work. In this case to display wordpress posts out of wordpress you will need to fetch post directly from wordpress database.

<?php

include “connection.php”;
$result=mysql_query(“SELECT * FROM wp_posts WHERE post_status = ‘publish’ ORDER BY ID DESC LIMIT 10″);
while($row = mysql_fetch_array($result))
{ echo “<li>”;
echo “<h2><a href=”.$row['guid'].”>”.$row['post_title'].”</a></h2>”;
echo “<p align=’justify’>”.nl2br($row['post_content']).”</p>”;
echo “</li>”;
echo “<hr>”;
}

?>

Explanation:

connection.php will have MySQL connection code for wordpress database in remote server. Assuming that your remote host supports remote mysql connections.

In next line we are querying 10 latest published post from wp_posts, assuming wp_ is your wordpress database prefix.

We have got the result, the code below it is just used to give it html format , which also includes nl2br, a function to convert new lines stored in database to HTML br tag equivalent.

If you get any problem using the code , feel free to comment. Analyzing wp_posts will give you more options for a posts to display. i.e. tags, alternate link etc..



Share with Friends ( Sharing is Caring! )


Subscribe For E-mail Notification Of Article Updates

Enter your email address:

NOTICE:
DO NOT FORGET TO CLICK ON THE VERIFICATION LINK AFTER SUBSCRIBING,THE VERIFICATION MAIL WILL BE SENT TO YOU FROM "FeedBurner Email Subscriptions".
( What is this? Why I need to do this?)

Related Posts

One response. Wanna say something?

  1. Saurabh
    Aug 30, 2012 at 14:53:43
    #1

    Really helpful Article,
    Thanks…

    [Reply]

Post a Comment

OR