ipsure logo
Logo and Language
Login icon Language selection icon
Hello, guest
*NIX BACKUP Hands-On blog header image Right block of Hands-On blog header image Final menu block of Hands-On blog header image
MS TIP PKI PROJECTS WORDPRESS Active category menu left background Active category menu right background Türkçe HANDS-ON SERVICES IT BUSINESS CONTACT ABOUT REFERENCES TERMS RSS
Home page Hands-On Services IT Business Contact About References Terms of Use RSS

29/01/2010

Displaying Most Recent Wordpress Posts On Another Web Site Or A Different WP Blog

Filed under: Wordpress — Tags: , , , , , — Sezgin Bayrak @ 01:31

You can use MySQL query snippet below if you want to display your most recently published Wordpress posts on another web site or a different WP blog which reside in same or another server, by expressing the publishing or modifying date, the title, content or the author. But before proceeding to the solution, let’s glance at the standart methods that have disadvantages which you might have refered as alternative solutions.

In order to achieve the intended purpose, you might have thought to use Wordpress Loop which is a powerfull tool indeed. Either you manage it with the standart query_posts or with WP_Query object which you can customize your own loops, you’ll face two important problems;

/* You need to call wp-load.php file from your blog (e.g.. www.yourdomain.com/blog)
 * to gain access to Wordpress blog content from external sites. You shall not need
 * this section of snippet when you use or include it inside your blog pages.
*/
<?php
// Include Wordpress
define('WP_USE_THEMES', false);
require('./blog/wp-load.php');
?>

/* WP Loop */
<?php
 $recentPosts = new WP_Query();
 $recentPosts->query('showposts=1');
?>
<?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
Last Updated: <?php the_modified_date(); ?><br>
<?php the_title(); ?><br>
Author: <?php the_author(); ?><br>
<a href="<?php the_permalink(); ?>"><img border=0 src="http://www.yourdomain.com/images/more.jpg" alt="more" width="60" height="18" /></a><br>
<?php endwhile; ?>
<?php rewind_posts(); ?>

First one of the problems is if you want to display your most recent posts as a preview on both a static web page and on the blog area (e.g. blog frontend) which your posts actually live, you’ll realize that the preview is being showed correctly on static pages but not inside the blog areas as a matter of the nature of  WP Loop. Contrarily you’ll see that older posts are listed right behind your recent post. As in the example above even you use the rewind_posts directive for recovering the situation, whenever you enter directly to a spesific post in your blog you’ll realize that the post itself shows up that you’re viewing inside the preview section instead of the recent one. The second problem is if you want to display the recent post of your blog inside a totally different blog, you’ll not be able to communicate with the other blog as looping blog only processes the operation inside its own database.

In order not to experience all these troubles I recommend you to use MySQL query snippet I prepared. In this way you can have your recent post displayed in any place in any design format as you wish. You can use this query placing it into any proper location that you choose inside you pages.

<?php
$host="localhost";
$username="username";
$password="password";
$database="wp_db_name";
mysql_connect($host,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
/* WP MySQL query */
$query = "SELECT *, DATE_FORMAT(post_date, '%Y-%m-%d %k:%i') AS post_date, DATE_FORMAT(post_modified, '%Y-%m-%d %k:%i') AS post_modified FROM wp_posts WHERE post_status = 'publish' ORDER BY id DESC LIMIT 1";
$result = mysql_query ($query) or die (mysql_error());
while ($row = mysql_fetch_array($result))
{
$date = $row['post_date'];
$modified = $row['post_modified'];
$title = $row['post_title'];
$excerpt = $row['post_excerpt'];
$id = $row['post_author'];
$link = $row['guid'];
}
$query2 = "SELECT display_name FROM wp_users WHERE ID=$id";
$result2 = mysql_query ($query2) or die (mysql_error());
while ($row2 = mysql_fetch_array($result2))
{
$author = $row2['display_name'];
}
?>
Last Updated: <?php echo "$modified"; ?><br>
<?php echo "$title"; ?><br>
Author: <?php echo "$author"; ?><br>
<a href="<?php echo "$link"; ?>"><img border=0 src="http://www.yourdomain.com/images/more.jpg" alt="more" width="60" height="18" /></a>

Certainly you may change the output by using $date instead of $modified or including $excerpt variable in your configuration. Also you may enrich the output visually by embedding these <?php echo commands inside the various table designs.

Related Posts with Thumbnails
Subscribe to our RSS feeds Email Subscription via FeedBurner RSS Subscription via FeedBurner

  No related posts.

7 Comments »

Trackbacks

There has not been any trackback links yet.

Reader Comments

atif19/05/2011 18:07

Hi,

Please tell me how to call 3 recent posts by using ur code..

thanks

Sezgin Bayrak09/06/2011 13:29

You may use the following snippet inside your php script;

$query = "SELECT *, DATE_FORMAT(post_date, '%Y-%m-%d %k:%i') AS post_date, DATE_FORMAT(post_modified, '%Y-%m-%d %k:%i') AS post_modified FROM wp_posts WHERE post_status = 'publish' ORDER BY id DESC LIMIT 3";
$result = mysql_query ($query) or die (mysql_error());

if(mysql_num_rows($result)) {
  echo '<ul>';
  while($post = mysql_fetch_assoc($result)) {
    echo '<li><a href="/blog/',$post['post_name'],'">',stripslashes($post['post_title']),'</a></li>';
  }
  echo '</ul>';
}

Play with the “DESC LIMIT” to set the number of the recent posts you want to call.

Terran11/08/2011 23:56

How to get the permalink instead of $link = $row['guid']?
Thanks

Terran12/08/2011 01:07

I found it stored in post_name finally.

Sezgin Bayrak12/08/2011 08:13

Exactly. You can use “post_name” to generate your own permalinks and make any number of recent posts listed by using a simple while loop as shown in my prior comment.

nikbanks07/11/2011 08:03

Hi, I’d like to display posts from one of my websites with titles, excerpts and featured image on another one of my website side by side in a column. I have been able to get your code to display a post but I am unable to show let’s see 3 posts displayed for example: post 1, post2, post 3 and i’m unable to show the featured image. Can you explain how to modify your code to do this? It would be a life saver.

Sezgin Bayrak09/11/2011 09:29

You can use the following snippet in order to get the excerpt of a post with its image. The said image had to be placed at the beginning of the post.

<div class="myexcerpt">
<?php
$excerpt = substr($content, 0, 590);
$excerpt = eregi_replace('(<img [^>]*>)', '<a href="/blog/' . $link . '" rel="bookmark" >' . '\1</a>', $excerpt);
echo "$excerpt"; ?><span class="myreadmore"><a href="<?php echo "/blog/$link"; ?>"> [...]</a></span>
</div>

However, displaying the excerpts side by side is cetainly a matter of html and CSS design. Also read Terran’s comments for the $link shift.

RSS feed for comments RSS feed for comments on this post. TrackBack URL

Leave a comment