{"id":45611,"date":"2022-06-03T10:52:40","date_gmt":"2022-06-03T10:52:40","guid":{"rendered":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/?p=45611"},"modified":"2022-06-03T10:52:57","modified_gmt":"2022-06-03T10:52:57","slug":"display-most-popular-post-without-using-plugin-in-wordpress","status":"publish","type":"post","link":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/display-most-popular-post-without-using-plugin-in-wordpress\/","title":{"rendered":"Display Most Popular Post Without Using Plugin in WordPress"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">There are numerous plugins available on the WordPress plugin directory for most popular posts. We are going to display the most popular post using the simple post query based on post views. We will be using post Meta to store the views count and we\u2019ll fetch the posts according to the views count using the stored Meta value. Sounds confusing but it\u2019s easier to implement.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It\u2019s not that hard. Adding a few lines of code on Snippets for Oxygen Or other pagebuilder in theme functions.php and modifying our template file will be enough.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Install and activate&nbsp;<a href=\"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/wordpress.org\/plugins\/code-snippets\/\">Code Snippets<\/a>&nbsp;plugin.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Go to Snippets > Add New.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">function count_post_visited() {\n    if( is_single() ) {\n        global $post;\n        $views = get_post_meta( $post->ID, 'post_viewed', true );\n        if( $views == '' ) {\n            update_post_meta( $post->ID, 'post_viewed', '1' );   \n        } else {\n            $views_no = intval( $views );\n            update_post_meta( $post->ID, 'post_viewed', ++$views_no );\n        }\n    }\n}\nadd_action( 'wp_head', 'count_post_visited' );<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Thereafter, paste the following wherever in your template files that you wish to display the popular posts:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">$popular_posts_args = array(\n    'posts_per_page' => 5,\n    'meta_key' => 'post_viewed',\n    'orderby' => 'meta_value_num',\n    'order'=> 'DESC'\n);\n$popular_posts_loop = new WP_Query( $popular_posts_args );\n  while( $popular_posts_loop->have_posts() ):\n    $popular_posts_loop->the_post();\n    \/\/ Loop continues\nendwhile;\nwp_reset_query();<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If you have any question then feel free ask me \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are numerous plugins available on the WordPress plugin directory for most popular posts. We are going to display the most popular post using the simple post query based on post views. We will be using post Meta to store the views count and we\u2019ll fetch the posts according to the views count using the [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":45612,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"slim_seo":{"title":"Display Most Popular Post Without Using Plugin in WordPress - Code DPlugins","description":"There are numerous plugins available on the WordPress plugin directory for most popular posts. We are going to display the most popular post using the simple po"},"_slim_seo_primary_term_category":0,"_slim_seo_primary_term_post_tag":0,"footnotes":""},"categories":[3],"tags":[50,71],"dpc_library":[],"class_list":["post-45611","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-wordpress","tag-custom-post","tag-most-popular"],"_links":{"self":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/45611","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/comments?post=45611"}],"version-history":[{"count":0,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/45611\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/media\/45612"}],"wp:attachment":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/media?parent=45611"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/categories?post=45611"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/tags?post=45611"},{"taxonomy":"dpc_library","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/dpc_library?post=45611"}],"curies":[{"name":"wp","href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/api.w.org\/{rel}","templated":true}]}}