Display Recent Posts Outside Your WordPress Blog Installation

Using WordPress for your site’s blog, but want to display the most recent post from it on your home page? You may have already discovered that doing this is harder than it should be. Fortunately, it’s far from impossible; this article shows you how.

In search engine optimization it is now becoming more important to have a user- friendly blog. Unlike the old days of SEO, when websites were mostly optimized for search engine bots, nowadays Google encourages webmasters to build user-friendly websites.

One of the most SEO-friendly blogging software packages is WordPress. It is not only a user-friendly blog offering, but allows for endless possibilities to customize the blog if you know its supported server side programming languages, such as PHP.

This article aims to solve one of the most common problems in websites using WordPress as a blog. You might have seen a lot of business establishments are using WordPress as their blogging platform, and they installed it as a folder in their root domain.

So for example, if your domain is http://www.thisisyourbusinesswebsite.com/ , WordPress is installed as a folder named “blog” with respect to the root directory, so it will be:

http://www.thisisyourbusinesswebsite.com/blog/

Of course, your most important page on your business website is the home page, which search engines expect to be user-friendly, content-rich and search engine friendly as well.

However, if you are using WordPress as a blog, you won’t be able to show or display the recent post directly on your business website’s home page, because it is now outside the WordPress blog installation.

Displaying the most recent WordPress post on your business website’s home page offers a lot of advantages. Take a look at the list:

1. It shows your latest content to your users, which increase audience engagement with your website’s content. For example, they will be reading the blog posts, commenting on your content, and understanding your services better, which then leads to them becoming your customer.

2. It provides easy ways for search engines to discover new content for your website, which helps with crawling and indexing, and contributes to long tail traffic.

3. The value and importance of your WordPress blog increases because it will earn some decent internal links coming directly from the home page.

4. Your old content will likely be crawled at a much faster rate than it would have been without internal links pointing to it.

Graphically, you would like the solution to have:

Your recent posts on your WordPress blog will be shown on the home page of your business website as “links” going to your WordPress blog. This is done using PHP to communicate with the WordPress MySQL database, in order to retrieve the latest post and display it as a hyperlink in the browser.

The above solution assumes that the business website’s content uses a database that is different from the MySQL database that the WordPress blog is using.

Ideally, the most recent posts are presented as links in the sidebar, so it would be best to edit the sidebar PHP source code of your main website template (which may NOT be using WordPress). The above solution also works if you have two WordPress installations on one domain (both for the main domain and your blog).

For better database management and efficiency, it would be great to separate the MySQL database for business website content from the actual blog content.

At the end of this tutorial you’ll find the actual PHP script that you will need to paste into your business website’s sidebar PHP template source code, which will do the actual work of retrieving the recent post from the WordPress MySQL database.

The recent post information (title tag and URL) can be found in the WordPress wp_post table in the MySQL database. If you look at the wp_post table in the MySQL database, you might ask: “How do I know which are the latest posts?”

The answer is simple: The latest post uses the highest post ID AND post_status should be set to “publish.” It would be ideal to present the top three most recent posts and use both PHP and MySQL queries to retrieve this from the wp_post table.

This screen shot link shows clearly the top three latest posts from a blog in wp_post table: http://www.php-developer.org/screenshot/recent_post_illustration.jpg. You might notice that the highest ID is 184, but since the post status is not set to “publish,” this is not the latest WordPress post. However, the post with ID=183 uses “publish” as the post status, so this is the latest post.

Also the second latest post should also use post_status set to “publish,” so it will be the post with ID= 181, and the third latest post uses ID=179.

Finally, the top three latest post titles will be:

First latest post – Best PHP websites and their characteristics

Second latest post – PHP SQL Max Basic Code to Communicate to MySQL

Third latest post – How to transfer Word press to New Host

Since the wp_post table has so many fields, not all fields are shown in the screen shot link provided. The next question is: “Now that I know the latest posts according to their ID numbers, post status and post title, how will I know the corresponding post URL?” The post URL can be found under the column named ”guid” in the wp_post table.

One of the more challenging questions is: “How do I formulate a MySQL query to retrieve the top three latest WordPress posts?” First, you will need to query MySQL to retrieve the latest post ID. The query in English will be: Select from table wp_post where post_ID is maximum AND post_status is publish  

The equivalent MySQL query statement will be: SELECT max(ID) from `wp_posts` where `post_status`=’publish’

Now that you know the maximum post ID, it will be easier to get the second latest post, which will be: “Select from table wp_post where post_ID is less than latest post ID and the post status is publish“. The equivalent MySQL query will be:

SELECT max(ID) from `wp_posts` where `post_status`=’publish’ and `ID`<’$maximum

Where $maximum is a PHP variable containing the value of the latest post. Based on the screen shot link provided earlier, the latest post ID is 183, so the maximum ID which is less than 184 and the post status set to “publish” is equal to 181 (since 182 uses “inherit” as the post_status).

The same technique will be used for the third latest post, for which the equivalent MySQL query will be: SELECT max(ID) from `wp_posts` where `post_status`=’publish’ and `ID`<’$middle’ 

Where $middle is the PHP variable containing the value of the second latest post ID.

The main reason you are retrieving the IDs of the top three latest posts is to be able to use the IDs to retrieve the equivalent post title and the post URL. For example, to retrieve the title of the latest post:

SELECT `post_title` FROM `wp_posts` WHERE `ID`=’$maximum’  

The query’s objective is to retrieve the post title where the ID is equal to $maximum (which is the latest post ID).

Now that you know the fundamentals of how to retrieve the top three latest posts from the wp_post table in a WordPress MySQL database, you are ready to write the PHP script. Below is the complete PHP script to retrieve the top three latest posts and present them as hyperlinks in the browser:

<?php

//connect to database

$username = "Put your WordPress database username here";

$password = "Put your WordPress database password here";

$hostname = "Put your database hostname here";

$database = "Put your database name here";

$dbhandle = mysql_connect($hostname, $username, $password)

 or die("Unable to connect to MySQL");

$selected = mysql_select_db($database,$dbhandle)

  or die("Could not select $database");

 

//find the latest post ID with post status set to publish

$resultmax = mysql_query("SELECT max(ID) from `wp_posts` where `post_status`=’publish’") or die(mysql_error());

$rowmax = mysql_fetch_array($resultmax) or die("Invalid query" . mysql_error());

$maximum = $rowmax['max(ID)'];

 

//find the second latest post ID with post status set to publish

$resultmiddle = mysql_query("SELECT max(ID) from `wp_posts` where `post_status`=’publish’ and `ID`<’$maximum’") or die(mysql_error());

$rowmiddle = mysql_fetch_array($resultmiddle) or die("Invalid query" . mysql_error());

$middle = $rowmiddle['max(ID)'];

 

//find the third latest post ID with post status set to publish

$resultmin = mysql_query("SELECT max(ID) from `wp_posts` where `post_status`=’publish’ and `ID`<’$middle’") or die(mysql_error());

$rowmin = mysql_fetch_array($resultmin) or die("Invalid query" . mysql_error());

$low = $rowmin['max(ID)'];

 

//mysql real escape string to prevent MySQL injection

$maximum=mysql_real_escape_string(stripslashes($maximum));

$middle=mysql_real_escape_string(stripslashes($middle));

$low=mysql_real_escape_string(stripslashes($low));

 

//retrieve title tags for latest post

$resulttitlemax = mysql_query("SELECT `post_title` FROM `wp_posts` WHERE `ID`=’$maximum’")

or die(mysql_error());

$rowtitlemax = mysql_fetch_array($resulttitlemax)

or die("Invalid query: " . mysql_error());

$titlemax = $rowtitlemax['post_title'];

 

//retrieve title for second latest post

$resulttitlemiddle = mysql_query("SELECT `post_title` FROM `wp_posts` WHERE `ID`=’$middle’")

or die(mysql_error());

$rowtitlemiddle = mysql_fetch_array($resulttitlemiddle)

or die("Invalid query: " . mysql_error());

$titlemiddle = $rowtitlemiddle['post_title'];

 

//retrieve title for third latest title tag

$resulttitlemin = mysql_query("SELECT `post_title` FROM `wp_posts` WHERE `ID`=’$low’")

or die(mysql_error());

$rowtitlemin = mysql_fetch_array($resulttitlemin)

or die("Invalid query: " . mysql_error());

$titlelow = $rowtitlemin['post_title'];

 

//retrieve URL for latest post

$resulturlmax = mysql_query("SELECT `guid` FROM `wp_posts` WHERE `ID`=’$maximum’")

or die(mysql_error());

$rowurlmax = mysql_fetch_array($resulturlmax)

or die("Invalid query: " . mysql_error());

$urlmax = $rowurlmax['guid'];

 

//retrieve URL for second latest post

$resulturlmiddle = mysql_query("SELECT `guid` FROM `wp_posts` WHERE `ID`=’$middle’")

or die(mysql_error());

$rowurlmiddle = mysql_fetch_array($resulturlmiddle)

or die("Invalid query: " . mysql_error());

$urlmiddle = $rowurlmiddle['guid'];

 

//retrieve URL for third latest post

$resulturlmin = mysql_query("SELECT `guid` FROM `wp_posts` WHERE `ID`=’$low’")

or die(mysql_error());

$rowurlmin = mysql_fetch_array($resulturlmin)

or die("Invalid query: " . mysql_error());

$urllow = $rowurlmin['guid'];

 

//display the most recent post as hyperlink in the browser

echo ‘<h3>Recent Blog Post</h3>’;

echo ‘<ul>’;

echo ‘<li><a href="’.$urlmax.’">’.$titlemax.’</a></li>’;

echo ‘<li><a href="’.$urlmiddle.’">’.$titlemiddle.’</a></li>’;

echo ‘<li><a href="’.$urllow.’">’.$titlelow.’</a></li>’;

echo ‘</ul>’;

?>

 

Implementation Tips

You will need to copy and paste the script into your sidebar PHP template in your main business website (not your WordPress blog); you can apply your own HTML style (color, font size, font face, header tags, etc) to match your template. Make sure the template uses a PHP extension. You will need to replace the database connection parameters with your own.

Below is a sample screen shot of the above script in action:

Google+ Comments

Google+ Comments