Show Popular Posts from WP-PostViews with a Custom Query

The WP-PostViews plugin provides a popular posts function and widget, which is very convenient. The only drawback is that the default output is just a simple list of post titles, which is a bit thin.

WP-PostViews stores the post view count in a custom field. Once we understand that, we can use a custom WordPress query to display popular posts ourselves.

<ul class="post-list">
<?php
$args = array(
    'meta_key' => 'views',
    'orderby' => 'meta_value_num',
    'order' => 'ASC'
);
$query = new WP_Query( $args );
?>
<li>
    <div class="icon-overlay"> <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('tiny'); ?></a> </div>
    <div class="meta">
        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
        <em><?php echo get_post_meta( get_the_ID(), 'views', true ); ?></em> </div>
    </li>
</ul>

The result returned by the query is a standard WordPress query result, so you can change the template markup however you like, including adding thumbnails or other custom fields.

Related Posts

Leave a Reply

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