{"id":135,"date":"2024-10-28T16:46:51","date_gmt":"2024-10-28T12:46:51","guid":{"rendered":"http:\/\/keksus.com\/?p=135"},"modified":"2025-01-05T21:52:10","modified_gmt":"2025-01-05T17:52:10","slug":"show-post-views-without-plugin","status":"publish","type":"post","link":"https:\/\/hostatto.com\/blog\/show-post-views-without-plugin.html","title":{"rendered":"Show post views without plugin"},"content":{"rendered":"<p>If you need to show\u00a0the number of posts\u00a0or pages on your WordPress site without using a plugin, you can write a simple function and add it to the <strong>functions.php<\/strong><br \/>\n<div class=\"codenote php\" style=\"background:#F39D77\">\n\t\t\t\t<strong>Wordpress<\/strong>\n\t\t\t<\/div><\/p>\n<pre class=\"lang:default decode:true \">function simple_post_views( $post_ID ) {\r\n\tglobal $count_key,$count;\r\n\t$count_key = 'simple_post_views_count';\r\n\t$count = get_post_meta( $post_ID, $count_key, true );\r\n\tif ( $count == '' ) {\r\n\t\t$count = 0; \r\n\t\tupdate_post_meta( $post_ID, $count_key, $count );\r\n\t\techo $count . __( ' views', 'your_textdomain' );\r\n\t} \r\n\telse {\r\n\t\tif ( is_single() || is_page() ) {\r\n\t\t\t$count++; \r\n\t\t}\t\t\r\n\t\tupdate_post_meta( $post_ID, $count_key, $count );\r\n\t\techo $count . __( ' views', 'your_textdomain' );\r\n\t}\r\n}<\/pre>\n<p>Now, in order to display the number of views, add this code to the single.php or page.php<\/p>\n<blockquote><p>&lt;?php simple_post_views( get_the_ID() ); ?&gt;<\/p><\/blockquote>\n<h2>Bonus<\/h2>\n<p>If you also want to show\u00a0the number of views in the WordPress admin dashboard, use this code:<br \/>\n <div class=\"codenote php\" style=\"background:#F39D77\">\n\t\t\t\t<strong>Wordpress<\/strong>\n\t\t\t<\/div><\/p>\n<pre class=\"lang:default decode:true \" >\/\/ show views for posts\r\nadd_action('manage_posts_custom_column', 'admin_postviews_column_content');\r\nadd_filter('manage_posts_columns', 'admin_postviews_column');\r\n\/\/ show views for pages\r\nadd_action('manage_pages_custom_column', 'admin_postviews_column_content');\r\nadd_filter('manage_pages_columns', 'admin_postviews_column');\r\n \r\nfunction admin_postviews_column( $defaults ) {\r\n    $defaults['viewscolumn'] = 'Views';\r\n    return $defaults;\r\n}\r\nfunction admin_postviews_column_content() {\r\n    simple_post_views( get_the_ID() );\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>...<\/p>\n","protected":false},"author":1,"featured_media":138,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11,54],"tags":[14,15,13],"class_list":["post-135","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tuts","category-wordpress","tag-function","tag-post-views","tag-tutorials"],"_links":{"self":[{"href":"https:\/\/hostatto.com\/blog\/wp-json\/wp\/v2\/posts\/135","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hostatto.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hostatto.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hostatto.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hostatto.com\/blog\/wp-json\/wp\/v2\/comments?post=135"}],"version-history":[{"count":0,"href":"https:\/\/hostatto.com\/blog\/wp-json\/wp\/v2\/posts\/135\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/hostatto.com\/blog\/wp-json\/wp\/v2\/media\/138"}],"wp:attachment":[{"href":"https:\/\/hostatto.com\/blog\/wp-json\/wp\/v2\/media?parent=135"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hostatto.com\/blog\/wp-json\/wp\/v2\/categories?post=135"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hostatto.com\/blog\/wp-json\/wp\/v2\/tags?post=135"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}