Skip to content Skip to sidebar Skip to footer

WordPress get popular posts without any plugins

Reading Time: < 1 minute

How can I get most popular posts without using any plugins in WordPress.I this post will describes how to get most popular posts without any plugins.

Method-1

Create a custom WordPress Query to get popular posts by limiting 10.

<ul class="popular-posts">
    <?php
        $popular_posts_list = new WP_Query('orderby=comment_count&posts_per_page=10');
        while ($popular_posts_list->have_posts()) {
        $popular_posts_list->the_post();
    ?>
    <li>
    <?php
    	  if ( has_post_thumbnail() ) { // check if the post has a post thumbnail.
    ?>
            <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
                <?php the_post_thumbnail('thumb', array('alt' => ''.get_the_title().'', 'title' => ''.get_the_title().'')); ?>
            </a>
            <a class="popular-post-title" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
    <?php
          } // end if
           else {
    ?> 		<a class="popular-post-title" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
    <?php
     	  } // end else
    ?>
     </li>
     <?php
        } // end while
     ?>
</ul>

$popular_posts_list = new WP_Query('orderby=comment_count&posts_per_page=10'); 
while ($popular_posts_list->have_posts()) { 
  $popular_posts_list->the_post(); 
}

 

Note
May be you have to displayed an error as following.

Fatal error: Call to undefined function: has_post_thumbnail() in …yourpage.php on line 32

So your theme has to call add_theme_support('post-thumbnails'); in an after_setup_theme action.
Otherwise the post-thumbnail functions aren’t included.
1-Go to /wp-content/themes/yourTheme folder.
2-Open the functions.php file.
3-Add add_theme_support('post-thumbnails'); at the end of the page or anywhere and save the file.
4-Reload your site.

Method-2

Simply executes the mysql query and get results.
That’s only!