[ad_1]
Hi @lola99,
This should help: How to sort posts by views.
If you have any further questions please let me know.
Thread Starter
lola99
(@lola99)
hi
I am not sure how to use this to display most popular posts by category…
Alright, follow steps 1 and 3 from the instructions posted here. When you’re done, add this to your theme’s functions.php file:
/**
* Sorts posts in category archive by views count.
*
* @param WP_Query $query
*/
function sort_category_archive_by_views($query) {
if ( $query->is_main_query() && $query->is_category() )
{
$query->set( 'orderby', 'meta_value_num' );
$query->set( 'meta_key', 'views_total' );
$query->set( 'order', 'DESC' );
}
}
add_action('pre_get_posts', 'sort_category_archive_by_views' );meta_key should be either views_total (sort by total views count) or views_weekly (sort by most viewed within the last 7 days).
Thread Starter
lola99
(@lola99)
good, it’s working fine, thanks!
Don’t mention it, glad I could help!
