Main Page
 The gatekeeper of reality is
 quantified imagination.

Stay notified when site changes by adding your email address:

Your Email:

Bookmark and Share
Email Notification
Project "Wordpress Blog Mod"
The purpose of this project is to demonstrate how to extract recent posts made to a Wordpress blog and have data available outside of Wordpress-recognized pages on your blog.

When you create PHP pages that are not recognized by Wordpress, none of the Wordpress functions or capabilities will work on those PHP pages even if they are in the active theme folder. The good news is, when you need to display blog data (in the case of this example, recent posts), it is not too difficult to retrieve since Wordpress uses mySQL (the storage location of the posts). Most of the time, a Wordpress blog installed on a website creates a configuration file (wp-config.php) which contain a myriad of settings you can reference. In this case, the example code will be used on both Wordpress recognized pages and pages not recognized by Wordpress; this means you will not be able to simply add the <?php include("/wp-config.php"); ?> code to the top of the custom PHP page designed to display recent posts since the custom PHP page is included into pages recognized by Wordpress as well as those not recognized.

The good news is you only need to look for three settings in the configuration file. The database server name, database name, and the username/password set up to access the database. Depending on how your website is setup you may be able to use $_ENV{DATABASE_SERVER} to get the database server name; unfortunately this environment variable is not usually set on Windows Servers running Wordpress and PHP. The next thing is to construct a query to get recent posts and then unroll the returned data and display it on the page. That's it!

$postTxtLen = 31; // Maximum number of text characters to allow in link text
$postMax = 10; // Maximum number of recent posts to return
/* Connect to mySQL Server */
$db_connector = mysql_connect("DATABASE SERVER NAME", "USERNAME", "PASSWORD") or die ('Connection to database server failed: ' . mysql_error());
/* Connect to mySQL Database */
mysql_select_db ("DATABASE NAME");
/* Create mySQL Query */
$query = "SELECT ID, post_date, post_modified, post_title, post_name, guid FROM wp_posts WHERE post_status = 'publish' AND post_type = 'post' AND post_author = 1 ORDER BY post_modified DESC LIMIT 0," . $postMax;
/* Grab Data Returned */
$result = mysql_query($query);
if (count($result) > 0) {
while($row = mysql_fetch_array($result)) {
$ID = $row["ID"];
$post_date = $row["post_date"];
$post_modified = $row["post_modified"];
$post_title = $row["post_title"];
$post_name = $row["post_name"];
$post_guid = $row["post_guid"];
$date_parts = explode(" ", $post_date);
$date_stamp = explode("-", $date_parts[0]);
$urlBuild = "/" . $date_stamp[0] . "/" . $date_stamp[1] . "/" . $date_stamp[2];
$urlBuild = $urlBuild . "/" . $post_name . "/";
print "<a href=\"" . $urlBuild . "\" target=\"_top\">";
if (strLen($post_title) > $postTxtLen) { print substr($post_title, 0, $postTxtLen) . "..."; }
else { print $post_title; }
print "</a>\r\n";
print "<br />\r\n";
else { print "No Recent Posts Were Found."; }
About Joe