Recent Posts Function

Technique #1

This function is useful when you need to display content, excerpt, custom fields, or anything related to the post beyond it's link and title. If you just need a list of linked titles, see the next technique. Put the following function in functions.php

function recent_posts($no_posts = 10, $excerpts = true) {

   global $wpdb;

   $request = "SELECT ID, post_title, post_excerpt FROM $wpdb->posts WHERE post_status = 'publish' AND post_type='post' ORDER BY post_date DESC LIMIT $no_posts";

   $posts = $wpdb->get_results($request);

   if($posts) {

               foreach ($posts as $posts) {
                       $post_title = stripslashes($posts->post_title);
                       $permalink = get_permalink($posts->ID);

                       $output .= '<li><h2><a href="' . $permalink . '" rel="bookmark" title="Permanent Link: ' . htmlspecialchars($post_title, ENT_COMPAT) . '">' . htmlspecialchars($post_title) . '</a></h2>';

                       if($excerpts) {
                               $output.= '<br />' . stripslashes($posts->post_excerpt);

                       $output .= '</li>';

       } else {
               $output .= '<li>No posts found</li>';

   echo $output;


After you've made the function. Put the following in the sidebar or wherever you like the recent posts to list..

<?php recent_posts(); ?>

You can give it 2 arguments, the first is the number of posts and the second is whether or not you want to display the excerpts. so recent_posts(2, false) will display the 2 most recent post titles.

Technique #2

<?php wp_get_archives( array(

    'type'            => 'postbypost',   // or daily, weekly, monthly, yearly
    'limit'           => 10,   // maximum number shown
    'format'          => 'html',   // or select (dropdown), link, or custom (then need to also pass before and after params for custom tags
    'show_post_count' => false,    // show number of posts per link
    'echo'            => 1     // display results or return array

) ); ?> 

Technique #3

More succinct version of #1, which also includes a more standardized query string.

   $recentposts = get_posts('numberposts=12&category=4');
   foreach ($recentposts as $post) :
       setup_postdata($post); ?>
       <li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
<?php endforeach; ?>


  1. User Avatar
    Permalink to comment#

    when i upgrade wordpress one version to another new version…

    is it the function.php is overwritten by updates..? and need to re-add the above code..??

    Please advice…

  2. User Avatar
    Permalink to comment#

    mans! weh dah dah la tu dow ng website ko.haha..

    @chris, thank for this man

  3. User Avatar
    Permalink to comment#

    quick question – how do you add the date to the function?

  4. User Avatar
    Permalink to comment#


  5. User Avatar
    Frank Waive

    @mansur, no it is not, the functions.php file is a part of your theme which is not updated during a wordpress update, it is only updated when u update your theme.

    The functions.php file will only be updated during a wordpress update if you’re using the default wordpress theme even if you may have customized it.

    If you’re currently using the default theme, the solution is renaming the theme from the style.css file, you should also rename the theme folder(very important!).

  6. User Avatar
    Permalink to comment#

    I am a newbie … and I would like to know how you even determine when to implement the above post … cause it sounds cool … but how do you know “WHEN”

  7. User Avatar
    Aaron Crow
    Permalink to comment#

    Ok I have a crazy question. A client of mine would like to display the latest 3 post on their home page. How would I go about breaking down each post into a seperate div for styling?

    The format would look something like below:

    Show Recent Post / Show Second Recent Post / Show Third Recent Post

    Each of the above are in separate DIV’s Hope that somewhat helps…

    Thanks in Advance,


  8. User Avatar
    Permalink to comment#

    its not working for my blog :(

    • User Avatar
      Permalink to comment#

      Open your Cpanel or FileZilla.
      Find functions.php hit edit. Insert the code provided above
      at the bottom of the page and hit save.
      Now go back to where you wanted your recent posts to appear
      and simply type:

      Hope it helps.

  9. User Avatar
    Frank Waive
    Permalink to comment#

    @interviewdate post a sample of the code you implemented, maybe i could help (that’s if you haven’t figured it out already)

  10. User Avatar
    Permalink to comment#

    please help me … I want to show recent posts in my index.php …. not in woordpress

    and this is the code i used i to input the posts

    but i didn’t know ho to get just the recent posts for eg the 8 recent posts…..


    $re= “select * from tableaux

    LIMIT 0 , 7″;

    $k = mysql_query($re);


    $t = substr($s[2] , 0 , 300 );
    $t=$t.” …”;
    echo ‘
    a href=”‘.$s[3].'”title=”‘.$s[1].'” ></a


  11. User Avatar
    Permalink to comment#

    Works great. But how can i show the “Post Date” ???

  12. User Avatar
    matka złodzieja
    Permalink to comment#

    where I should put the code <?php recent_posts(); ?> ?

  13. User Avatar
    Permalink to comment#

    Thanks very much these tutorials and loops are great

  14. User Avatar
    Permalink to comment#

    To include the date just do this:

    In the function replace this
    $request = "SELECT ID, post_title, post_excerpt FROM $wpdb-&gt;posts WHERE post_status = 'publish' AND post_type='post' ORDER BY post_date DESC LIMIT $no_posts";
    with this

    $request = "SELECT ID, post_title, post_excerpt, post_date FROM $wpdb-&gt;posts WHERE post_status = 'publish' AND post_type='post' ORDER BY post_date DESC LIMIT $no_posts";

    and just add it to your output a bit like this
    $output .= '<a href="' . $permalink . '" title="Permanent Link: ' . htmlspecialchars($post_title, ENT_COMPAT) . '" rel="nofollow">' . htmlspecialchars($post_title) . '(' . htmlspecialchars($post_date) . ')' . '</a>';

    • User Avatar
      Permalink to comment#

      Or to format your date in the format of 24th April 2014 change post_date in the query to DATE_FORMAT( post_date, '%D %M %Y' ) AS post_date

Leave a Comment

Posting Code!

You may write comments in Markdown. This makes code easy to post, as you can write inline code like `<div>this</div>` or multiline blocks of code in triple backtick fences (```) with double new lines before and after.

Code of Conduct

Absolutely anyone is welcome to submit a comment here. But not all comments will be posted. Think of it like writing a letter to the editor. All submitted comments will be read, but not all published. Published comments will be on-topic, helpful, and further the discussion or debate.

Want to tell us something privately?

Feel free to use our contact form. That's a great place to let us know about typos or anything off-topic.