Title: The PHP code for the last 5 posts
Last modified: August 18, 2016

---

# The PHP code for the last 5 posts

 *  Resolved [klifix](https://wordpress.org/support/users/klifix/)
 * (@klifix)
 * [20 years ago](https://wordpress.org/support/topic/the-php-code-for-the-last-5-posts/)
 * Hello,
 * Can someone please help me with this?
 * This is the problem:
 * On my frontpage (www.klifix.nl) I have insert to my sidebar the last 5 posts 
   on the frontpage.
    I also want the last 5 posts of movies.klifix.nl & radio.klifix.
   nl on my frontpage to appear. The question is: How do have to edit the following
   php-code to do this?
 * The code:
 * <?php
    $today = current_time(‘mysql’, 1);
 * if ( $recentposts = $wpdb->get_results(“SELECT ID, post_title FROM $wpdb->posts
   WHERE post_status = ‘publish’ AND post_date_gmt < ‘$today’ ORDER BY post_date
   DESC LIMIT 15”)):
    ?>
    -  <li id=”recents”>
 *  -  <?php
       foreach ($recentposts as $post) { if ($post->post_title == ”) $post-
      >post_title = sprintf(__(‘Post #%s’), $post->ID); echo ”
    - <font color=#000099><b>* </b></font>ID’>”;
       the_title(); echo ‘
    - ‘;
    - }
       ?>
 * <?php endif; ?>

Viewing 15 replies - 1 through 15 (of 78 total)

1 [2](https://wordpress.org/support/topic/the-php-code-for-the-last-5-posts/page/2/?output_format=md)
[3](https://wordpress.org/support/topic/the-php-code-for-the-last-5-posts/page/3/?output_format=md)
[4](https://wordpress.org/support/topic/the-php-code-for-the-last-5-posts/page/4/?output_format=md)
[5](https://wordpress.org/support/topic/the-php-code-for-the-last-5-posts/page/5/?output_format=md)
[6](https://wordpress.org/support/topic/the-php-code-for-the-last-5-posts/page/6/?output_format=md)
[→](https://wordpress.org/support/topic/the-php-code-for-the-last-5-posts/page/2/?output_format=md)

 *  [geoffe](https://wordpress.org/support/users/geoffe/)
 * (@geoffe)
 * [20 years ago](https://wordpress.org/support/topic/the-php-code-for-the-last-5-posts/#post-393466)
 * So you have three WordPress installations on the same server?
 * The $wpdb object will reference only the database of the WP installation at [http://www.klifix.nl](http://www.klifix.nl),
   which I presume is running on the same database as movies.klifix.nl but has a
   different table prefix.
 * If this is the case, all you need to fix is the $wpdb->posts in your SQL query
   to the name of the table for posts at movies.klifix.nl
 * Perhaps it is wp_movies_posts, so then you would want
    “SELECT ID, post_title
   FROM wp_movies_posts WHERE post_status = ‘publish’ AND post_date_gmt < ‘$today’
   ORDER BY post_date DESC LIMIT 15”
 *  Thread Starter [klifix](https://wordpress.org/support/users/klifix/)
 * (@klifix)
 * [20 years ago](https://wordpress.org/support/topic/the-php-code-for-the-last-5-posts/#post-393470)
 * and if it’s on a different server?
 *  [davidchait](https://wordpress.org/support/users/davidchait/)
 * (@davidchait)
 * [20 years ago](https://wordpress.org/support/topic/the-php-code-for-the-last-5-posts/#post-393479)
 * Easiest is to use an rss aggregator (like my CG-FeedRead plugin), and use it 
   to grab and list the last N entries in each site’s RSS feed. that’s fast and 
   cached, and doesn’t require writing SQL queries yourself. 😉
 *  Thread Starter [klifix](https://wordpress.org/support/users/klifix/)
 * (@klifix)
 * [20 years ago](https://wordpress.org/support/topic/the-php-code-for-the-last-5-posts/#post-393484)
 * I succeed! Thanx very much!!!
 *  [geoffe](https://wordpress.org/support/users/geoffe/)
 * (@geoffe)
 * [20 years ago](https://wordpress.org/support/topic/the-php-code-for-the-last-5-posts/#post-393485)
 * yes, you’d likely find the best ease with an rss aggregator.
 * But how (or why) would you have your subdomains on a different web server?
 * While I don’t know anything of mr. chait’s plugin, I have found [this plugin](http://soderlind.no/archives/2004/11/08/aggrss-an-rss-aggregator/)
   to be very handy. You’ll probably want to test a couple (there are many out there)
   to see what works for you.
 *  Moderator [Samuel Wood (Otto)](https://wordpress.org/support/users/otto42/)
 * (@otto42)
 * WordPress.org Admin
 * [20 years ago](https://wordpress.org/support/topic/the-php-code-for-the-last-5-posts/#post-393540)
 * And then there’s always the codex: [http://codex.wordpress.org/Function_Reference/fetch_rss](http://codex.wordpress.org/Function_Reference/fetch_rss)
 *  Thread Starter [klifix](https://wordpress.org/support/users/klifix/)
 * (@klifix)
 * [20 years ago](https://wordpress.org/support/topic/the-php-code-for-the-last-5-posts/#post-393565)
 * I have a few different websites. From some websites i get a few or more databases.
   I use a few of 6 databases for my website. That’s why, but for Klifix I just 
   use 2 databases, and it works fine!
 *  Thread Starter [klifix](https://wordpress.org/support/users/klifix/)
 * (@klifix)
 * [20 years ago](https://wordpress.org/support/topic/the-php-code-for-the-last-5-posts/#post-393567)
 * By the way: Thanx very much for the good help!!!
 *  [geoffe](https://wordpress.org/support/users/geoffe/)
 * (@geoffe)
 * [20 years ago](https://wordpress.org/support/topic/the-php-code-for-the-last-5-posts/#post-393587)
 * so it is different databases, not different servers?
 * That would mean that you could just query the other database fairly easy, but
   using rss might be simpler for you, if not as direct a method.
 *  Thread Starter [klifix](https://wordpress.org/support/users/klifix/)
 * (@klifix)
 * [20 years ago](https://wordpress.org/support/topic/the-php-code-for-the-last-5-posts/#post-393603)
 * Maybe it is! I will check it out!
 * I have a second question about this. Next to the last 5 posts i want to have 
   de date & time of the posts. How can i do that in the last 5 posts?
 *  [geoffe](https://wordpress.org/support/users/geoffe/)
 * (@geoffe)
 * [20 years ago](https://wordpress.org/support/topic/the-php-code-for-the-last-5-posts/#post-393630)
 * well, you’re calling the posts from your other WP installation through an RSS
   feed?
 * All the data you can have is in [your feed](http://movies.klifix.nl/feed/).
 * So if you were using [fetch_rss](http://codex.wordpress.org/Function_Reference/fetch_rss),
   much like how you call the link to the post by $item[‘link’], you would call 
   your date on the post by $item[‘pubDate’]. Then you could format that date using
   [strtotime](http://ca.php.net/manual/en/function.strtotime.php) and the [date](http://ca.php.net/manual/en/function.date.php)
   functions.
 * So like this:
    $thetime = strtotime($item[‘pubDate’]); $thetime = date(‘Y-m-d
   H:i’, $thetime ); echo $thetime;
 * See the format characters on the [date](http://ca.php.net/manual/en/function.date.php)
   page for other options in formatting.
 *  Thread Starter [klifix](https://wordpress.org/support/users/klifix/)
 * (@klifix)
 * [20 years ago](https://wordpress.org/support/topic/the-php-code-for-the-last-5-posts/#post-393645)
 * Thanks for your respons!
 * I’m not calling from rss feed, instead I use the above mentioned php code. Where
   do I have to put the timecode in this code?
 *  [geoffe](https://wordpress.org/support/users/geoffe/)
 * (@geoffe)
 * [20 years ago](https://wordpress.org/support/topic/the-php-code-for-the-last-5-posts/#post-393666)
 * sorry, I don’t know what PHP code you are referring to.
 * are you making a query to the database of your other site to get posts?
 *  Thread Starter [klifix](https://wordpress.org/support/users/klifix/)
 * (@klifix)
 * [20 years ago](https://wordpress.org/support/topic/the-php-code-for-the-last-5-posts/#post-393676)
 * Sorry, this one:
 * <?php
    $today = current_time(‘mysql’, 1);
 * if ( $recentposts = $wpdb->get_results(“SELECT ID, post_title FROM $wpdb->posts
   WHERE post_status = ‘publish’ AND post_date_gmt < ‘$today’ ORDER BY post_date
   DESC LIMIT 15”)):
    ?>
 * <li id=”recents”>
    <?php foreach ($recentposts as $post) { if ($post->post_title
   == ”) $post->post_title = sprintf(__(‘Post #%s’), $post->ID); echo “ <font color
   =#000099><b>* </b></font>ID’>”; the_title(); echo ‘ ‘;
 * }
    ?>
 * <?php endif; ?>
 *  [geoffe](https://wordpress.org/support/users/geoffe/)
 * (@geoffe)
 * [20 years ago](https://wordpress.org/support/topic/the-php-code-for-the-last-5-posts/#post-393686)
 * Ok, just change
    $wpdb->get_results(“SELECT ID, post_title FROM to $wpdb->get_results(“
   SELECT ID, post_title, post_date FROM
 * and then in your loop call it using $post->post_date when you want to print it.

Viewing 15 replies - 1 through 15 (of 78 total)

1 [2](https://wordpress.org/support/topic/the-php-code-for-the-last-5-posts/page/2/?output_format=md)
[3](https://wordpress.org/support/topic/the-php-code-for-the-last-5-posts/page/3/?output_format=md)
[4](https://wordpress.org/support/topic/the-php-code-for-the-last-5-posts/page/4/?output_format=md)
[5](https://wordpress.org/support/topic/the-php-code-for-the-last-5-posts/page/5/?output_format=md)
[6](https://wordpress.org/support/topic/the-php-code-for-the-last-5-posts/page/6/?output_format=md)
[→](https://wordpress.org/support/topic/the-php-code-for-the-last-5-posts/page/2/?output_format=md)

The topic ‘The PHP code for the last 5 posts’ is closed to new replies.

## Tags

 * [front page](https://wordpress.org/support/topic-tag/front-page/)
 * [last](https://wordpress.org/support/topic-tag/last/)
 * [posts](https://wordpress.org/support/topic-tag/posts/)
 * [truncate](https://wordpress.org/support/topic-tag/truncate/)

 * 78 replies
 * 10 participants
 * Last reply from: [Kafkaesqui](https://wordpress.org/support/users/kafkaesqui/)
 * Last activity: [19 years, 6 months ago](https://wordpress.org/support/topic/the-php-code-for-the-last-5-posts/page/6/#post-393908)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
