WordPress Tips & Tricks

Add a Popular Posts Widget in WordPress (No Plugin)

A popular posts widget points new readers to the articles your audience already loves and keeps strong content visible long after publish day. The surprise for many site owners is that WordPress has no built in popularity ranking, so a plugin free widget starts with one honest question: popular according to what?

This guide uses a clear signal. You will rank posts by stored view counts with WP_Query, show the result as a shortcode or a real sidebar widget, cache the list so it stays fast, and style it with a few lines of CSS.

What does popular mean in WordPress?

WordPress stores posts, dates, authors, and comments, but it does not track which posts people read most. There is no popularity score in the database waiting for you to display. Any popular posts list needs a signal that you choose and maintain.

Three signals cover most sites. View counts measure reading directly. Comment counts measure discussion. Recent posts measure freshness and work as a fallback while real data builds up. This guide uses view counts as the main signal, with the other two as fallbacks.

Why do you need a view counter first?

A ranking needs numbers, and view counts supply them. The companion guide on how to add a post views counter in WordPress without a plugin stores each total in a post meta field named post_views. This widget reads that exact field, so the counter records views and the widget ranks them.

If your site has no counter yet, add it first and let counts build for a week or two.

How do you build the popular posts list with WP_Query?

Before adding code, take a full backup and test the snippet on a staging copy first. Add the PHP to your child theme functions.php file or to the Code Snippets plugin. Never edit a parent theme, because the next theme update would erase your work.

The core is one query. It asks for published posts, sorts by the post_views meta value from highest to lowest, and returns the top five. A helper function then turns the result into a clean HTML list with titles and view counts.

<?php
function vtg_get_popular_posts( $count = 5 ) {
    $args = array(
        'post_type' => 'post',
        'posts_per_page' => $count,
        'meta_key' => 'post_views',
        'orderby' => 'meta_value_num',
        'order' => 'DESC',
        'ignore_sticky_posts' => true,
    );
    return new WP_Query( $args );
}

function vtg_popular_posts_html( $count = 5 ) {
    $query = vtg_get_popular_posts( $count );
    if ( ! $query->have_posts() ) {
        return '<p>No popular posts yet.</p>';
    }
    $html = '<ul class="vtg-popular-posts">';
    while ( $query->have_posts() ) {
        $query->the_post();
        $views = (int) get_post_meta( get_the_ID(), 'post_views', true );
        $html .= '<li><a href="' . esc_url( get_permalink() ) . '">' . esc_html( get_the_title() ) . '</a>';
        $html .= ' <span>' . number_format_i18n( $views ) . ' views</span></li>';
    }
    $html .= '</ul>';
    wp_reset_postdata();
    return $html;
}

Each argument earns its place. The meta_key argument points at the post_views field. The orderby value meta_value_num sorts values as numbers, so 100 views correctly outranks 9 views. The order value DESC puts the highest count first, and ignore_sticky_posts stops a sticky post from jumping the queue.

The esc_url and esc_html functions clean the link and title before output. The wp_reset_postdata call restores the main page data after the custom loop, so your article and comments keep working. The reference at developer.wordpress.org documents every argument used here.

Can the widget exclude the post being read?

Yes. On single post pages, listing the current article in its own sidebar looks odd. Add a post__not_in argument with the current post ID, and the query fills the space with the next most viewed article instead.

How do you add the list as a shortcode?

A shortcode is the quickest route from code to visible list. Register one shortcode that calls your HTML helper. The count attribute lets you change the list length without touching code again.

<?php
add_shortcode( 'popular_posts', 'vtg_popular_posts_shortcode' );
function vtg_popular_posts_shortcode( $atts ) {
    $atts = shortcode_atts( array( 'count' => 5 ), $atts, 'popular_posts' );
    return vtg_popular_posts_html( (int) $atts['count'] );
}

With that snippet active, placing [popular_posts count=”5″] inside a Shortcode block in your sidebar widget area shows the list. You can move or remove the block in seconds, and the shortcode also works inside a post or page.

How do you turn the list into a real sidebar widget?

The full method registers a proper widget in the Widgets screen and the Customizer. The class extends WP_Widget, prints the theme widget wrappers, and reuses the same HTML helper, so both methods show identical results.

<?php
class VTG_Popular_Posts_Widget extends WP_Widget {
    public function __construct() {
        parent::__construct( 'vtg_popular_posts', 'Popular Posts (No Plugin)' );
    }
    public function widget( $args, $instance ) {
        echo $args['before_widget'];
        echo $args['before_title'] . 'Popular Posts' . $args['after_title'];
        echo vtg_popular_posts_html( 5 );
        echo $args['after_widget'];
    }
}
add_action( 'widgets_init', function() {
    register_widget( 'VTG_Popular_Posts_Widget' );
} );

Open Appearance and then Widgets, and drag your new widget into any widget area your theme provides. If widgets are new territory, our guide to WordPress widgets explained for beginners covers widget areas and placement in plain language.

Where should the popular posts widget appear?

A sidebar keeps the list visible beside every article. A footer reaches readers at the end of a page. A below content placement catches readers the moment they finish one article and want another. Start with one placement, watch clicks for a few weeks, and expand only when the data justifies it.

If your theme lacks the widget area you want, our guide on how to add a custom sidebar in WordPress without a plugin shows how to register a new area and display it where you need it.

How do you style the widget without slowing the page?

A few lines of CSS turn the plain list into a tidy sidebar feature. Remove default bullets, add gentle spacing, and show the view count as a quiet line under each title. The class name from the PHP output gives you a clean styling hook.

.vtg-popular-posts {
    list-style: none;
    margin: 0;
    padding: 0;
}
.vtg-popular-posts li {
    padding: 8px 0;
    border-bottom: 1px solid #e5e5e5;
}
.vtg-popular-posts span {
    display: block;
    font-size: 0.85em;
    color: #666666;
}

Add the CSS through the Customizer additional CSS panel or your child theme stylesheet. For thumbnails, print a small image inside the loop with get_the_post_thumbnail and style it with a fixed width. Keep thumbnails small, because the widget appears on many pages. Our web hosting guide explains what to look for when pages feel sluggish.

How do you cache the popular posts list?

Without caching, the ranking query runs on every page load. The query is small, but a busy sidebar still adds up. A transient stores the finished HTML for six hours and serves the stored copy until it expires.

<?php
function vtg_popular_posts_cached( $count = 5 ) {
    $cached = get_transient( 'vtg_popular_posts_html' );
    if ( false !== $cached ) {
        return $cached;
    }
    $html = vtg_popular_posts_html( $count );
    set_transient( 'vtg_popular_posts_html', $html, 6 * HOUR_IN_SECONDS );
    return $html;
}

Point your shortcode and widget at the cached function. Rankings rarely change minute by minute, so six hours stays fresh enough while removing repeated work. For an instant refresh after a major publish, delete the transient with delete_transient and the next load rebuilds the list.

What should new sites show before view counts build up?

A new site has no view history, so a views based ranking shows an empty list or a tie between zeros. Use a fallback instead. When counts are missing or all zero, order by comment_count. When comments are scarce too, show recent posts until real data accumulates.

  • Views available: rank by post_views from highest to lowest.
  • Views missing but comments exist: rank by comment_count.
  • Neither signal yet: show the latest published posts.
  • Nothing published: show a friendly empty message, never a blank gap.

This staged approach means the widget never looks broken, and the ranking becomes more accurate as your counter collects real numbers.

How is a popular posts widget different from related posts?

A popular posts widget asks what your whole audience reads most, and it shows the same winners across the site. Related posts ask what resembles the article being read right now, usually by shared categories or tags, so that list changes on every post. Strong blogs often run both. Our guide on how to add related posts in WordPress without a plugin builds the second half of that pairing.

How do you test the widget before you keep it live?

Testing prevents the two classic surprises: a list stuck in the wrong order, and a blank widget on a live page. Run these checks on staging first.

  • Open a few posts several times and confirm the order changes with the counts.
  • Check a single post page and confirm the current post is excluded if enabled.
  • Clear the transient once and confirm the list rebuilds without errors.
  • Test the empty state on a fresh install with no counted views.
  • View the widget at phone width and confirm titles wrap cleanly.

If the order looks frozen, the transient is usually the reason. Delete it once and reload. If counts appear blank, confirm your counter writes to the post_views meta key exactly, since a mismatched key returns empty values.

Frequently Asked Questions

Does WordPress track post views by default?

No. WordPress records comments and publish dates, but it does not count post views on its own. You need a small counter snippet that stores views in post meta, which the companion views counter guide sets up for this widget.

Should I use the shortcode method or the widget class?

Use the shortcode inside a Shortcode block for the fastest setup. Use the widget class when you want a proper entry in the Widgets screen with theme styled titles. Both share the same query and output.

How often does the popular posts ranking update?

With the transient cache in place, the ranking refreshes every six hours. That keeps the list current without running the query on every page load. Delete the transient manually for an immediate refresh.

Can the widget show posts from one category only?

Yes. Add a category argument to the query array with the category ID or slug you want, and the ranking limits itself to that category. Category specific lists suit larger sites with distinct sections.

Last updated: December 2026.

Hostinger - Get 20% OFF

Habib ur Rehman

Hello! I'm the founder of VU Tech Geek. As a BSCS graduate and experienced WordPress professional, I'm passionate about technology and education. My Virtual University journey has given me valuable insights and knowledge, which I love to share with fellow students.… More »

Related Articles

Leave a Reply

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

Back to top button

Adblock Detected

Please consider supporting us by disabling your ad blocker