The function getPostViews() is retrieving the total number of views for each post while setPostViews() increases the post views counter each time post is viewed.
You need to call setPostViews() somewhere in your single.php and then use below code to retrieve posts on basis of posts view count.
$args = Array(
'post_type' => 'post',
'posts_per_page' => '5',
'meta_key' => 'post_views_count',
'orderby' => 'meta_value_num',
'order' => 'DESC'
);
$popular = new WP_Query( $args );
<?php while($popular->have_posts()) : $popular->the_post(); ?>
<?php the_post_thumbnail(); ?> <?php the_title(); ?> <?php the_content(); ?>
<?php endwhile ?>
Related Posts:
- Setting posts_per_page for taxonomy term template
- Send post id through ajax and get the post content back
- restrict incrementation of post vies count when refreshing the page
- How to change the case of all post titles to “Title Case”
- How to detect single.php (but not single-portfolio.php)?
- Function to execute when a post is moved to trash .
- Changing the post date and time with function
- Get current post id in functions.php
- Dynamically add id to heading tags
- Post/Page Preview Template
- How to get Images included in Post
- disable WP automatically inserted line breaks after an image
- Getting the post_id in wp_ajax function
- Fatal error: Call to undefined function post_exists()
- How to publish a post with empty title and empty content?
- delete post also attachments
- How to call function within a page/post, to dynamically generate content?
- My ‘post attributes’ is missing from my WordPress install
- Restrict users from editing post based on the age of the post
- How do I use element instead of tags in WordPress post content having webP support?
- posts page – different lengths of excerpt
- Writing a view count with w3 total cache
- Code to auto expire posts after 30 days
- List only child categories a post is in, of a specific parent category
- Forcing oembeds to top of post
- Add link on the top menu of the post table?
- Keep a fluid max number of posts on site
- Accessing $post variable from template part
- How to change post status from publish to draft using hook in wordpress?
- Guest Author – How to modify my custom function code if the guest author URL will follow a particular pattern/format?
- how to show all post in my page-grid.php template page
- Show post titles only on the homepage
- Custom colors for post rows based on post meta value
- How to add an admin function only to posts, not pages?
- Use template of parent category for single post sub-categories
- loop not displaying posts on custom template page
- Human Time Diff, change mins to minutes
- How to get new post URL?
- Force update all posts in custom post type, no content changes
- Custom wp_trim_words() function not trimming right
- Disable REST API for a user ROLE
- How to display post tags
- How to Display Post View Count
- Remove double space after a period
- Add confirmation popup on “Move to Trash”
- Allow role to delete posts but block him the wp-admin
- Including text at the end of every post
- Why does `url_to_postid` return 0 when testing `page_for_posts` Page?
- custom single.php not working
- WP_Query in functions.php overrides global $post object, even with wp_reset_query()
- Delete Post From Front-End and attachment permanently
- Adding variable for post title so post template can include auto-generated link to view on external website.com/{post title}
- Limit function to specific post category
- $post object is null
- How to display different single post template based on author?
- Use template for posts with a particular category grandparent
- When paginating a Page with the tag, how can the 2nd and subsequent page styles be customised?
- How only display all post related to category
- Function to show random posts from a category
- Is it possible to have two different previews of a post (ie. two templates for one post)?
- How to change the page break URL from ../post/2 to …/post/page2/
- Hiding posts in a list from specified categories
- Double count view in archive.php
- How can I list random authors from current post category?
- Disable single post page
- Sorting posts according to view counts not working
- Display page content AFTER a loop of posts
- Different template for posts of all subcategories of category
- Enabling post thumbnails for custom post type
- Run a function when post is deleted?
- Checking is user author of number of posts?
- Custom Post Templates
- Single post as homepage?
- Create cron job without a plugin?
- Templating of a specific post ID
- Getting current post ID in functions.php
- How to rename “Publish” metabox title in post screen
- Category Template: Need to display different content on first page of archives
- Make view count for pop up post
- Change post_date to post_modified date on post template?
- Action hook for new pending posts?
- What part of template to edit to remove category name from the top of posts?
- Uppercase first sentence in every post
- Add icon/badge next to posttitle of specific category
- Ajax posts filter by date, comments, top views, top likes
- Prepend or add an Image to the content of a Post
- Converting a dynamic piece of code using WordPress Loop into a static one using Post ID
- Why content_arr[‘extended’] removes paragraph tags?
- How can I pass the ID of a post to a page template by clicking a url?
- Load post attached images on a single page site with fancybox
- Using custom post template
- Exclude category from
- What is the filter or hook to add admin controls to posts on the front end?
- API endpoint to get most-read and popular posts
- Change post order random through out the entire WordPress
- How to display the link (title) and thumbnail post?
- Force the “Choose from the most used tags” meta box section to always be expanded
- Show the content that is after the excerpt using child theme
- How can I get global $post to work for CPT and update user?
- How to allow users to post only in certain category and hide elements from edit page?