Archived topic
Display most visited posts WP_Query
1 reply · Started by Hilton on July 24, 2020
Hello,
I currently use a WP_Query to display the most recent articles using a shortcode.
The WP_Query is this one:
$the_query = new WP_Query( array(
'cat' => $category->term_id,
'posts_per_page' => 5,
'order' => 'desc',
'orderby' => 'modified'
)
I would like to know if, to display the most visited articles instead of the most recent ones, I could use this WP_Query here:
$the_query = new WP_Query( array(
'cat' => $category->term_id,
'posts_per_page' => 5,
'meta_key' => 'post_views_count',
'orderby' => 'meta_value_num'
)
Thanks
Hi there,
You could, but you'd need something to add to that custom field count each time someone visits your site.
Something like this may help: https://wordpress.org/plugins/advanced-page-visit-counter/