Download PHP Code to display Recent Post on any post or page with Shortcode [recent_post]

Read about the shortcode to show Recent Post on any post or page with Shortcode.

As we have usually seen WordPress provides us with a widget that we can use very easily in the place of Sidebar and Widget.

The problem is that those methods don’t work for displaying recent posts inside Posts, Pages, and custom post types. Like inside the post content itself. For that, we can use a Recent Post Shortcode. Along with this, we can also increase the functionality of Recent Post List such as Custom Post Type, Change Post List, Post Number, Custom Category, List Order, Change List OrderBy, etc.

PHP Code For Recent Post Shortcode

You can display a list of related posts in a WordPress post editor (eg, RTE/Visual/TinyMCE or a plain-text editor) by adding the following piece of code to your theme’s functions.php file.

/* Recent Post Shortcode */
function my_recent_post_shortcode($atts, $content = null) {
    global $post;

       'post_type' => 'post' ,    // Custom Post Type
       'cat' => '',               // Choose custom Category
       'num' => '5',             // No of Post
       'order' => 'DESC',        // List Order
       'orderby' => 'post_date', // Order By
    ), $atts));

    $args = array(
      'post_type' => $post_type,
      'cat' => $cat,
      'posts_per_page' => $num,
      'order' => $order,
      'orderby' => $orderby,

    $output = '';
    $posts = get_posts($args);
    foreach($posts as $post) {
      $output .= '<li><a href="'. get_the_permalink() .'">'. get_the_title() .'</a></li>';


   return '<ul class="recent-post">' . $output . '</ul>' ;
add_shortcode('recent_posts', 'my_recent_post_shortcode');

This code creates a Recent Post Shortcode that fetches a customizable set of posts from WordPress’ database, and simply displays them on your post or page. You can use this code without any modification.

The code for this Recent Post Shortcode can be added to any WordPress theme, or alternatively, the code can also be added to your site via a simple plugin.

How To Use

To use Recent Post Shortcode, add the following code to your WordPress page or post (where Recent Post is to be displayed):-

/* Default Recent Post Shortcode */

Note:- This code will display the latest five posts with the default setting of Recent Post Shortcode.

Other Settings of Recent Post Shortcode

Custom Post Type

You can change the post_type to page, post or any other custom post type.

/* Use Custom Post Type */
[recent_posts post_type="page"]

Change Post Category

If you want to display the post of a particular category through Recent Post Shortcode, then you can easily display the post list by using the ID of that category. For example, if your category ID is => 5 then you would write the code like this :-

/* choose custom category */
[recent_posts cat="5"]

Change No of Post

You can increase or decrease the number of posts in Recent Posts as per your need. The default number of posts in this shortcode is set to 5. You can use the following code to change it:-

/* Choose no of post in Recent Post */
[recent_posts num="15"]

Change Post List Order

Ascending Order (ASC)

If you want to display the post list in ascending order (‘A, B, C….’ or ‘A, B, C..’ or ‘1, 2, 3…’) then the following Codes can be used:-

/* Choose list Order */
[recent_posts order="ASC"]
Descending order (DESC)

If you want to display the post list in descending order (‘Z, Y, X….’ or ‘J, Q, T..’ or ‘9, 8, 7……’) If yes, then the following code can be used:-

/* Choose list Order */
[recent_posts order="DESC"]

Change List Order by

  • ‘ID’ – by post id.
  • ‘author’ – according to the author.
  • ‘title’ – according to the Title.
  • ‘name’  – according to the Post Slug.
  • ‘post_date‘ – according to the Post Published date.
  • ‘modified’ – as of Post update date.
  • ‘parent’ – Order by post/page parent id.
  • ‘rand’  – Random order.
  • ‘comment_count’  – According to the comment number of the post.
/* Recent Post Shortcode with custom Setting */
[recent_posts num="15" cat="" order="asc" orderby="rand" post_type="page"]

Code Explanation

This code from Recent Posts is written by combining WordPress’s add_shortcode() function with get_posts() . You can modify the code as per your convenience. The supporting articles for the changes are as follows:-

    This Post Has 4 Comments

    1. Bhaumik

      Very Nice

    2. Aman

      I’d like to thank you for the efforts you’ve put in penning this blog.
      I really hope to view the same high-grade content from
      you in the future as well. In truth, your creative writing
      abilities has encouraged me to get my own,
      personal site now 😉

    Leave a Reply