Query to display the 5 most viewed or most popular posts
Webmaster talk
 21 Jan '24
 36 |  0 

To filter the most viewed posts, you can use the `WP_Query` class in WordPress. Here is an example of how to query for the top 5 most viewed posts ¹:

$query = new WP_Query( array(
‘meta_key’ => ‘post_views_count’,
‘orderby’ => ‘meta_value_num’,
‘posts_per_page’ => 5
) );

This code will return the top 5 most viewed posts based on the `post_views_count` meta key. The `meta_value_num` parameter sorts the posts by the number of views in descending order ¹.

I hope this helps!

Source: Conversation with Bing, 1/21/2024
(1) How to query for most viewed posts and show top 5. https://wordpress.stackexchange.com/questions/49330/how-to-query-for-most-viewed-posts-and-show-top-5.
(2) query_posts() – Function | Developer.WordPress.org. https://developer.wordpress.org/reference/functions/query_posts/.
(3) How to Display Popular Posts by Views in WordPress (2 Ways) – WPBeginner. https://www.wpbeginner.com/wp-tutorials/how-to-track-popular-posts-by-views-in-wordpress-without-a-plugin/.
(4) undefined. http://codex.wordpress.org/Class_Reference/WP_Query.

Related Posts
The cost of a trip

Travel webmasters use various factors to calculate the price of a trip to Brussels. The cost of a trip depends Read more


Leave a Reply

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

Car inspections & auction inspections.