Random posts, Related posts and Recent posts in WordPress without using a plugin

by Sam  


There are many ways we display our posts on wordpress. It could be random, recent, or related posts . Adding of these features increases the possibility of the users to visit the website for a longer period of time. On this page, I will show you how to display your posts without using a plugin.

Random posts

Let’s say, you want to display random posts in your wordpress website. Well, it is just a simple and easy thing to do.

All you have to do is open your file wherever you like to display your random post and paste the following code below:


<h2 class="sidebar-title">Random Posts</h2>
<ul>
<?php
$args = 'orderby=rand&numberposts=15';
$posts = get_posts($args); foreach($posts as $post) { ?>
<li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></li>
<?php } ?>
</ul>

From the code above, orderby=rand in line 4, is a significant string value. This code will generate the random post.

Related posts

Another important feature for a website is to display related posts. Sometimes, users are not satisfied with the content they see on a page and need to view more related posts. Hence, displaying related posts encourages users to become interested in visiting the website.

As a web developer, I also find related post as a significant feature on my website. I was looking for such code in wordpress until I found this code below:


<?php
$tags = wp_get_post_tags($post->ID);
if ($tags) {
echo 'Related Posts';
$first_tag = $tags[0]->term_id;
$args=array(
'tag__in' => array($first_tag),
'post__not_in' => array($post->ID),
'showposts'=>5,
'caller_get_posts'=>1
);
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
<?php
endwhile;
}
}
?>

To implement this code into your website, open a file where you want to display your related posts, example, single.php, and paste it right away.

Recent Posts

Lastly, you might want to display recent posts in your wordpress website. Just like the above codes, you don’t need a plugin to display the recent posts in your website. Just copy and paste the code below into the page where you want to display the recent posts.


<h2>Recent Posts</h2>
<ul>
<?php
$data = array( 'numberposts' => '20' );
$recent_posts = wp_get_recent_posts( $data);
foreach( $recent_posts as $recent ){
echo '<li><a href="' . get_permalink($recent["ID"]) . '">' . $recent["post_title"].'</a> </li> ';
}
wp_reset_query();
?>
</ul>

In order to view more parameters you might want to put into your recent posts, check this link or see the code below:

$args = array(
	'numberposts' => 10,
	'offset' => 0,
	'category' => 0,
	'orderby' => 'post_date',
	'order' => 'DESC',
	'include' => '',
	'exclude' => '',
	'meta_key' => '',
	'meta_value' =>'',
	'post_type' => 'post',
	'post_status' => 'draft, publish, future, pending, private',
	'suppress_filters' => true
);

I hope these codes are also useful to you. If you have something to ask, add more feature or suggest anything, please feel free to comment.



Leave a Reply

Your email address will not be published. Required fields are marked *


  1. Bryan says:

    Hello,

    Thank you for posting. This is a big help.

    I would like to ask a question. How about if I am going to limit the number of posts?

    thanks

    1. samuel says:

      Hello Bryan,

      Which posts are you referring? Kindly change the number below to your preferred item to be displayed.

      Random post
      $args = ‘orderby=rand&numberposts=15’; //line 4

      Related Posts
      ‘showposts’=>5, //line 9

      Cheers,

  2. almasad ibrahime says:

    Thanks for this. But, I have a problem with recent post since it also displays the “Draft” recent posts. I want only to display “Published” post. ty

    1. samuel says:

      Hello almasad,

      Just add ‘post_status’ => ‘publish’ into your $args array.

      example:
      $args = array( ‘numberposts’ => ’20’ , ‘post_status’ => ‘publish’ );

      Best regards,

Need of a Website?

Maybe you're planning to build a landing page for your website. You might also have a plan to create a web application. I can offer you with an affordable but with good quality services. Don't hesitate to submit your specifications by clicking the button below.

Get Quote