How to Display Most View Post in the template file?

I need to create most viewed post column in the template file, but based on post view count? Function File function wpb_get_post_views($postID){ $count_key = 'wpb_post_views_count'; $count = get_post_meta($postID, $count_key, true); if($count==''){ delete_post_meta($postID, $count_key); add_post_meta($postID, $count_key, '0'); return "0 View"; } return $count.' Views'; } Template File <?php $popularpost = new WP_Query( array( 'posts_per_page' => 4, 'meta_key' => 'wpb_post_views_count', 'orderby' => 'meta_value_num', 'order' => 'DESC' ) ); while ( $popularpost->have_posts() ) : $popularpost->the_post(); the_title(); endwhile; ?>
Category: Web

API endpoint to get most-read and popular posts

I am reading Endpoint reference exposed from wordpress api but I don't see an endpoint to get most read post(s) and popular post. There is an endpoint to retreive all posts https://example.com/wp-json/wp/v2/post. Is there an endpoint or query parameter(s) missing to get most read post(s) and popular post without use a wordpress plugin (wordpress beginner) ?
Category: Web

How do display most popular post from a year earlier to the day?

I'm trying to display a post that was the most popular a year ago on the day it was posted. So today would be October 12, 2019. I would like to base it off of this: functions.php: function setPostViews($postID) { $countKey = 'post_views_count'; $count = get_post_meta($postID, $countKey, true); if($count==''){ $count = 0; delete_post_meta($postID, $countKey); add_post_meta($postID, $countKey, '0'); }else{ $count++; update_post_meta($postID, $countKey, $count); } } loop template: setPostViews(get_the_ID()); <?php query_posts('meta_key=post_views_count&orderby=meta_value_num&order=DESC'); if (have_posts()) : while (have_posts()) : the_post(); ?> <li><a href="<?php the_permalink(); …
Category: Web

popular post for week and month

I have a code and can not use week or month filter for it. Please check the code and tell me how can I display popular post by week or months <?php $popularpost = new WP_Query(array( 'posts_per_page' => $cat, 'meta_key' => 'post_views_count', 'orderby' => 'meta_value_num', 'order' => 'DESC', )); while ( $popularpost->have_posts() ) : $popularpost->the_post(); ?>
Category: Web

Best approach to create Hot and Trending sections

I am going to implement a Hot and Trending section on a website. I have seen such sections on many popular websites but can't really figure out how exactly this works. Although I assumed Hot section is most popular posts (based on post views) which I developed by a custom WP_Query. And it works. $args = array( 'post_type' => 'post', 'post_status' => 'publish', 'meta_key' => 'post_views', 'orderby' => 'meta_value_num', 'ignore_sticky_posts' => 1, 'paged' => $paged, ); $hot_query = new WP_Query( …
Category: Web

Sort Popular Posts by Views for the Last Week

I'm trying to sort the popular posts so it shows the most visited in the last week, but it hasn't worked. Anyone have an idea of why it isn't working? <?php $popularpost = new WP_Query( array ( 'posts_per_page' => 5, 'ignore_sticky_posts' => 1, 'meta_key' => 'sw_post_views_count', 'orderby' => 'meta_value_num', 'order' => 'DESC', 'date_query' => array ( array ( 'year' => date( 'Y' ), 'week' => date( 'W' ), ), ), ) ); while( $popularpost->have_posts() ) : $popularpost->the_post(); ?>
Category: Web

Wordpress popular posts by week code

I m trying make a code that list the popular posts by week. Until now i was able to creat a veiws counter and display the 4 popular posts. So what i still need is to make this query more accurate. I would like get the thumbnails and links and get the most popular of the week. Functions.php remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0); function wpb_track_post_views ($post_id) { if ( !is_single() ) return; if ( empty ( $post_id) ) { global …
Category: Web

Create a list-style post of popular posts for the week?

I've been looking for something that would automate the creation of a popular posts of the week blog post. So for instance, every Monday create post that is a list of the most popular posts from the last 7 days. And it would probably need to be able to limit the time scope, so say it was an old post that was getting the most traffic, I'd only want to highlight posts that were published in the last 7 days. …
Category: Web

Popular posts by view with Jetpack

I'm using Jetpack to retrieve the number of views for each blog post. Is there a way to use this to generate a call to the most viewed posts and list them? I've been wanting to do that for a long time now and I've looked into various plugins, but I'd prefer to hardcode this. Is there a way to write a code so that I could utilize the view count provided by Jetpack? I looked at similar questions here, …
Category: Web

Show the most popular post per week

The plugin Most Popular Posts doesn't support WPML and therefore I have tried creating my own. I have found this tutorial in creating your own code for showing the most popular posts on my site: How to Display Popular Posts by Views in WordPress without a Plugin However this doesn't take in the factor of per week. I would like it to be pointed in the right direction on how to do this. This code updates the posts actual view-count: …
Category: Web

Popular Post By Month in WordPress

I have been trying to get popular posts for the current month. I wrote the code given here. But showing nothing. <?php $query_args = array( 'posts_per_page' => 3, 'post_type' => 'post', 'date_query' => array( array( 'year' => date( 'Y' ), 'month' => date( 'm' ), ), ), 'meta_key' => 'wpb_post_views_count', 'orderby' => 'meta_value_num', 'order' => 'DESC', ); $the_query = new WP_Query( $query_args ); ?> <?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?> <div class="col-lg-4 col-md-4 …
Category: Web

Show Top 10 posts in a rss feed

I am trying to get a rss feed that will show the top 10 posts for the week. I am using the top 10 popular posts plugin to show them in a sidebar on the site which works great but need to show them in a rss so that I can have them send in a email that goes out. Below is some code for the top 10 API. Any help would be great. <?php /* * This example fetches …
Category: Web

Display most popular posts of category

Right now in my slider I'm showing most popular post by views. However, I want to shop most popular post by views only for a certain category? The category id I want to use is "10". How would I achieve this? Thanks in advance. <div id="slider-food"> <?php $carousel_cat = get_theme_mod('carousel_setting','1'); $carousel_count = get_theme_mod('count_setting','4'); $month = date('m'); $year = date('Y'); $new_query = new WP_Query( array('posts_per_page' => $carousel_count, 'meta_key' => 'wpb_post_views_count', 'orderby' => 'meta_value_num', 'order' => 'DESC','monthnum'=>$month,'year'=>$year )); ?> <?php if ( …
Category: Web

popular post weekly and monthly

i want to display popular posts for 1 week ago and 1 month ago.these are codes that i used but these don't work correctly for 1 week ago <ul> <?php $week = date('W'); $year = date('Y'); query_posts('meta_key=post_views_count&cat='.$link1.'&posts_per_page=9&orderby=meta_value_num&order=DESC&year=' . $year . '&weeknum=' . $week); while (have_posts()): the_post(); ?> <li> <h2><a href="<?php the_permalink(); ?>" target="_blank"><?php the_title(); ?></a></h2> <div style="display:<?php echo $display;?>" class="tooltiptext hidden-xs"><?php the_excerpt(); ?></div> </li> <?php endwhile; wp_reset_query(); ?> </ul> for 1 month ago <ul> <?php $month = date('m'); $year = …
Category: Web

Display query post based on two orderby value in wordpress

I have been trying to display popular posts (based on view counts) and fetching them randomly using array in orderby but it doesn't seem to be working. The reason I want them to display randomly is because I don't want the same post to be displayed repeatedly to both new & repeated visitors as the views count increases naturally. Here is the code I tried: $popularpost = new WP_Query( array( 'category' => 'comedy', 'posts_per_page' => 4, 'meta_key' => 'post_views_count', 'orderby' …
Category: Web

Get a page ancestor from a most viewed list

I'm using WPBeginner's tutorial do display a list of the most viewed pages on a site. This is on my functions.php: function wpb_set_post_views($postID) { $count_key = 'wpb_post_views_count'; $count = get_post_meta($postID, $count_key, true); if($count==''){ $count = 0; delete_post_meta($postID, $count_key); add_post_meta($postID, $count_key, '0'); }else{ $count++; update_post_meta($postID, $count_key, $count); } } //To keep the count accurate, lets get rid of prefetching remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0); this is on my page.php (inside the loop): <?php wpb_set_post_views(get_the_ID()); ?> and this is in a custom …
Category: Web

Show popular post in another php website via WP REST JSON API

I need to show popular & recent posts in another PHP web site under the same domain. Example: www.example.com -> main website (php, mysql) www.example.com/blog -> WordPress blog Need to show popular, recent posts of blog in the main website. Please note that blog and main website use two separate database. I decided to use the WP REST JSON API plugin for that. Now I have different question. I use following code to retrieve last week popular post. What I …
Category: Web

Display most popular in the past two days

Hi I'm using this code below to display the most popular post on my wordpress site. It works great! I was wondering if there was anyway to show the most popular post of the current day? This in my functions.php function getPostViews($postID){ $count_key = 'post_views_count'; $count = get_post_meta($postID, $count_key, true); if($count==''){ delete_post_meta($postID, $count_key); add_post_meta($postID, $count_key, '0'); return "0 View"; } return $count.' Views'; } function setPostViews($postID) { $count_key = 'post_views_count'; $count = get_post_meta($postID, $count_key, true); if($count==''){ $count = 0; delete_post_meta($postID, …
Category: Web

Most popular post for last 7 days

I need to change the most popular post widgets with date restrictions. For an example, how should I get most popular posts(post_views_count) of last 7 days. original query, $popularposts = new WP_Query('showposts=10&meta_key=post_views_count&orderby=meta_value_num&order=DESC&ignore_sticky_posts=1') then I changed that to, $args = array( 'date_query' => array( array( 'after' => '-7 days' ) ), 'showposts' => 10, 'ignore_sticky_posts' => 1, 'meta_key' => 'post_views_count', 'orderby' => 'meta_value_num', 'order' => 'DESC' ); $the_query = new WP_Query( $args ); However it doesn't give any results. How should …
Category: Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.