Sort query loop by post view count for logged out users
The following code does the following:
- Creates a custom field named
post_views_count
. - When a logged out visits a posts, the visit counter is incremented by
1
. - Query loop displays most visited posts in a descending order.
- Any posts with no visits get added to the query after the visited posts in a published date descending order.
Just insert the code to your child theme’s functions.php file. Insert your query loop and add the class of trending-articles
to the query loop grid block.