{"id":2445,"date":"2017-10-23T09:11:12","date_gmt":"2017-10-23T09:11:12","guid":{"rendered":"http:\/\/docs.contentviewspro.com\/?p=2445"},"modified":"2024-01-02T10:13:17","modified_gmt":"2024-01-02T10:13:17","password":"","slug":"show-popular-wordpress-posts","status":"publish","type":"docs","link":"https:\/\/contentviewspro.com\/documentation\/article\/show-popular-wordpress-posts\/","title":{"rendered":"Show the most popular WordPress posts"},"content":{"rendered":"<blockquote><p>This feature is only available in the <a href=\"https:\/\/www.contentviewspro.com\/#buynow\" rel=\"noopener noreferrer\" target=\"_blank\">Pro version<\/a>.<\/p><\/blockquote>\n<p>As a website owner, you might want to display the most popular WordPress posts in a beautiful layout. Most popular posts have most number of visits.<\/p>\n<h2>1. Count number of visits<\/h2>\n<p>WordPress doesn\u2019t have a built-in feature to count the number of visits for each post. You can choose one of two custom ways below to count visits of posts:<\/p>\n<ul>\n<li>Use another plugin<br \/>\nThere are some WordPress plugins were created to implement this feature, one of them is <a href=\"https:\/\/wordpress.org\/plugins\/wp-postviews\/\" rel=\"nofollow noopener noreferrer\" target=\"_blank\">WP-PostViews<\/a>. Please install &#038; activate that plugin.<\/li>\n<li>Use custom coding<br \/>\nIn case you want to make it work without using extra plugin, please add this code to file <strong>functions.php<\/strong> in the theme&#8217;s folder (or install this plugin <a href=\"https:\/\/wordpress.org\/plugins\/code-snippets\/\" rel=\"nofollow noopener noreferrer\" target=\"_blank\">Code Snippets<\/a> then add this code to <a href=\"https:\/\/ps.w.org\/code-snippets\/assets\/screenshot-2.png\" rel=\"nofollow noopener noreferrer\" target=\"_blank\">the &#8220;Code&#8221; textarea<\/a>):<\/p>\n<pre style=\"font-size:13px\">\n\/** Content Views Pro - Enqueue script to count visits, support cache plugin *\/\nadd_action( 'wp_enqueue_scripts', 'cv_theme_count_pviews_js', PHP_INT_MAX );\nfunction cv_theme_count_pviews_js() {\n\tif ( !is_singular() ) {\n\t\treturn;\n\t}\n\n\tif ( !wp_script_is( 'jquery', 'done' ) ) {\n\t\twp_enqueue_script( 'jquery' );\n\t}\n\n\t\/\/ Must use \"jquery-migrate\"\n\twp_add_inline_script( 'jquery-migrate', 'jQuery(document).ready(function(){\tjQuery.ajax({type:\"GET\",url:\"' . admin_url( 'admin-ajax.php' ) . '\",data:\"post_id=' . $GLOBALS[ 'post' ]->ID . '&action=cv_count_pviews\",cache:0});});' );\n}\n\n\/** Content Views Pro - Count visits *\/\nadd_action( 'wp_ajax_cv_count_pviews', 'cv_theme_count_pviews_php' );\nadd_action( 'wp_ajax_nopriv_cv_count_pviews', 'cv_theme_count_pviews_php' );\nfunction cv_theme_count_pviews_php() {\n\tif ( !isset( $_GET[ 'post_id' ] ) )\n\t\treturn;\n\n\t$post_id = intval( $_GET[ 'post_id' ] );\n\tif ( $post_id > 0 ) {\n\t\tupdate_post_meta( $post_id, 'views', (int) get_post_meta( $post_id, 'views', true ) + 1 );\n\t\texit();\n\t}\n}\n\n\/** Content Views Pro - add default visit count 1 to all posts *\/\nadd_action( 'admin_head', 'cvp_theme_add_default_visit_count' );\nfunction cvp_theme_add_default_visit_count() {\n\tif ( !get_option( 'cvp_theme_add_visit_count' ) ) {\n\t\tglobal $wpdb;\n\t\t$wpdb->query( \"INSERT INTO {$wpdb->postmeta} (post_id,meta_key,meta_value)\n  SELECT ID, 'views', 1\n  FROM {$wpdb->posts} WHERE {$wpdb->posts}.post_type = 'post' AND {$wpdb->posts}.post_status = 'publish'\" );\n\n\t\tadd_option( 'cvp_theme_add_visit_count', 1, '', 'no' );\n\t}\n}\n<\/pre>\n<\/li>\n<\/ul>\n<h2>2. Create a View to show popular posts<\/h2>\n<p>After finishing above, here are some easy steps to show most popular WordPress posts with Content Views Pro:<\/p>\n<ul>\n<li>Open your homepage (or any another post or page) to start counting<\/li>\n<li>In Admin area, click Content Views > Add New to start.<\/li>\n<li>Select the <strong>Sort by<\/strong> option in the Filter Settings tab, Advance section of the View<\/li>\n<li>In the &#8220;Sort by&#8221; group, click <strong>Add New<\/strong> button, then select options as below image:<br \/>\n&nbsp;<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/contentviewspro.com\/documentation\/wp-content\/uploads\/2017\/10\/CVP-show-most-popular-posts.png\" alt=\"CVP - show most popular posts\" width=\"451\" height=\"396\" class=\"alignnone size-full wp-image-2448\" srcset=\"https:\/\/contentviewspro.com\/documentation\/wp-content\/uploads\/2017\/10\/CVP-show-most-popular-posts.png 451w, https:\/\/contentviewspro.com\/documentation\/wp-content\/uploads\/2017\/10\/CVP-show-most-popular-posts-300x263.png 300w\" sizes=\"auto, (max-width: 451px) 100vw, 451px\" \/><\/li>\n<\/ul>\n<p>Now you can add that View to where you want to show the most popular posts.<\/p>\n<blockquote><p>Notice: At the beginning, the View might return no posts. Because of there are no posts have the <code>views<\/code> custom field. When someone visits your posts, the <code>views<\/code> custom field will be created and updated, and you will start seeing the popular posts.<\/p><\/blockquote>\n<p>Best regards,<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This feature is only available in the Pro version. As a website owner, you might want to display the most popular WordPress posts in a beautiful layout. Most popular posts have most number of visits. 1. Count number of visits WordPress doesn\u2019t have a built-in feature to count the number of visits for each post. &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"\" href=\"https:\/\/contentviewspro.com\/documentation\/article\/show-popular-wordpress-posts\/\"> <span class=\"screen-reader-text\">Show the most popular WordPress posts<\/span> Read More &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"site-sidebar-layout":"default","site-content-layout":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","footnotes":""},"doc_category":[55],"doc_tag":[],"class_list":["post-2445","docs","type-docs","status-publish","hentry","doc_category-advanced-modification"],"year_month":"2026-09","word_count":500,"total_views":"7","reactions":{"happy":"0","normal":"0","sad":"0"},"author_info":{"name":"admin1","author_nicename":"admin1","author_url":"https:\/\/contentviewspro.com\/documentation\/author\/admin1\/"},"doc_category_info":[{"term_name":"Advanced Modification","term_url":"https:\/\/contentviewspro.com\/documentation\/cats\/advanced-modification\/"}],"doc_tag_info":[],"_links":{"self":[{"href":"https:\/\/contentviewspro.com\/documentation\/wp-json\/wp\/v2\/docs\/2445","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/contentviewspro.com\/documentation\/wp-json\/wp\/v2\/docs"}],"about":[{"href":"https:\/\/contentviewspro.com\/documentation\/wp-json\/wp\/v2\/types\/docs"}],"author":[{"embeddable":true,"href":"https:\/\/contentviewspro.com\/documentation\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/contentviewspro.com\/documentation\/wp-json\/wp\/v2\/comments?post=2445"}],"version-history":[{"count":2,"href":"https:\/\/contentviewspro.com\/documentation\/wp-json\/wp\/v2\/docs\/2445\/revisions"}],"predecessor-version":[{"id":5720,"href":"https:\/\/contentviewspro.com\/documentation\/wp-json\/wp\/v2\/docs\/2445\/revisions\/5720"}],"wp:attachment":[{"href":"https:\/\/contentviewspro.com\/documentation\/wp-json\/wp\/v2\/media?parent=2445"}],"wp:term":[{"taxonomy":"doc_category","embeddable":true,"href":"https:\/\/contentviewspro.com\/documentation\/wp-json\/wp\/v2\/doc_category?post=2445"},{"taxonomy":"doc_tag","embeddable":true,"href":"https:\/\/contentviewspro.com\/documentation\/wp-json\/wp\/v2\/doc_tag?post=2445"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}