William
William

Reputation: 1030

How can I display the Most Viewed posts in WordPress?

I'd like to display the 10 most viewed posts in WordPress using WP_Query but the code I have doesn't display anything.

Code:

$q_mostViewed = [
    'meta_key' => 'post_views_count',
    'orderby' => 'meta_value_num',
    'order' => 'DESC',
    'posts_per_page' => '10'
];

Could you please help me?


Full code:

<!-- Most Viewed -->

    <div class="home-post-wrapper col-sm-12 nopadding">

        <?php
        $q_mostViewed = [
            'meta_key' => 'post_views_count',
            'orderby' => 'meta_value_num',
            'order' => 'DESC',
            'posts_per_page' => '10'
        ];
        $mostViewed = new WP_Query($q_mostViewed);
        if ($mostViewed->have_posts()) :
            while ($mostViewed->have_posts()) :
                $mostViewed->the_post(); ?>
                  // Do things here
            <?php endwhile; 
        endif; ?>
    </div>

Upvotes: 6

Views: 12692

Answers (2)

JalalJaberi
JalalJaberi

Reputation: 2617

First, you need to create a meta to count the views. Here on wordpress.stackexchange.com I explained how to do that. BTW, this is the code:

add_filter('manage_posts_columns', function($columns) {
    return array_merge($columns, ['post_views' => 'Views']);
});

add_action('manage_posts_custom_column', function($column) {
    if ($column === 'post_views') {
        $count = get_post_meta(get_the_ID(), 'post_views_count', true);
        echo empty($count) ? 'no views :(' : "$count view :)";
    }
});

function jj_count_post_views($content)
{
    if (is_single() && !is_preview()) {
        $postId = get_the_ID();

        update_post_meta(
            $postId,
            'post_views_count',
            (int) get_post_meta($postId, 'post_views_count', true) + 1
        );

        remove_filter('the_content', 'jj_count_post_views');
    }

    return $content;
}

add_filter('the_content', 'jj_count_post_views');

Then you need to query posts based on that meta. It will be like this:

$posts = get_posts(array(
    'post_type'        => 'post',
    'numberposts'      => 3,
    'post_status'      => 'publish',
    'suppress_filters' => false,
    'meta_key' => 'post_views_count',
    'orderby' => 'meta_value_num',
    'order' => 'DESC',
));

Upvotes: 1

Ivan Barayev
Ivan Barayev

Reputation: 2055

Open the functions.php file of the activated theme and add the following code.

setPostViews() function add or update the post meta with post_views_count meta key.

/*
 * Set post views count using post meta
 */
function setPostViews($postID) {
    $countKey = 'post_views_count';
    $count = get_post_meta($postID, $countKey, true);
    if($count==''){
        $count = 0;
        delete_post_meta($postID, $countKey);
        add_post_meta($postID, $countKey, '0');
    }else{
        $count++;
        update_post_meta($postID, $countKey, $count);
    }
}

single.php File

Open the single.php file from activated theme directory and place the setPostViews() function inside the loop.

setPostViews(get_the_ID());

Display the Most Viewed Posts

The following query will fetch the posts based on the post_views_count meta key value. Place the following code in the sidebar or where you want to display the most popular posts list.

<?php
query_posts('meta_key=post_views_count&orderby=meta_value_num&order=DESC');
if (have_posts()) : while (have_posts()) : the_post();
?>
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php
endwhile; endif;
wp_reset_query();
?>

Upvotes: 7

Related Questions