{"id":889,"date":"2006-11-25T19:15:25","date_gmt":"2006-11-26T03:15:25","guid":{"rendered":"http:\/\/www.krunk4ever.com\/blog\/?p=889"},"modified":"2007-11-06T00:35:42","modified_gmt":"2007-11-06T08:35:42","slug":"view-count","status":"publish","type":"post","link":"https:\/\/www.krunk4ever.com\/blog\/2006\/11\/25\/view-count\/","title":{"rendered":"View Count"},"content":{"rendered":"<p>So I&#8217;ve installed a new plug-in called <a href=\"http:\/\/wunder-ful.com\/wordpress-plugins\">View Count<\/a> by <a href=\"http:\/\/wunder-ful.com\/\">Michael O&#8217;Connell<\/a>. Was pretty simple. Getting the view count was the 1st step in what I was trying to accomplish. I wanted to add a list of the top viewed posts in my sidebar. Installing the plug-in was pretty simple as I just dropped the folder and file into my WordPress wp-content\\plugins directory.<\/p>\n<p>Once activated, there was a new column in my wp_posts table called view_count. The default value is 0, but this&#8217;ll change once this plug-in is ran for some time.<\/p>\n<p>So the next step was to create the MySQL query to get the x most viewed posts. Turns out executing the query was easier than I thought:<br \/>\n<code>$results = $wpdb-&gt;get_results(\"SELECT ID, post_title, view_count FROM $wpdb-&gt;posts ORDER BY view_count DESC LIMIT $limit\" );<\/code><\/p>\n<p>Then for each result in the array of results you got back, I did the following which links to the post with title and displays how many views it has:<br \/>\n<code>echo '&lt;li&gt;&lt;a href=\"' . get_permalink( $result-&gt;ID ) . '\"&gt;' . $result-&gt;post_title . '&lt;\/a&gt; [&lt;strong&gt;' . $result-&gt;view_count . '&lt;\/strong&gt;]&lt;\/li&gt;';<\/code><\/p>\n<p>And that&#8217;s basically it!<\/p>\n<p>I did notice 1 problem with with the plug-in&#8217;s instruction. It states to show your posts sorted by view count, to just add this link: <a href=\"\/blog\/?ordrby=view_count\">?ordrby=view_count<\/a>, however, the correct link according to the code does have the &#8216;e&#8217; which is <a href=\"\/blog\/?orderby=view_count\">?orderby=view_count<\/a>.<\/p>\n<p><ins datetime=\"2006-11-26T03:34:10+00:00\">Apparently, it&#8217;s also counting when I&#8217;m writing\/editing the post. I frequently save due to times in the past when I lost complete essays by accidentally closing the tab or if my browser crashes and each reload counts as 1 view. I&#8217;ve changed it so it ignores my views, so it seems like a good workaround for now.<\/ins><\/p>\n","protected":false},"excerpt":{"rendered":"<p>So I&#8217;ve installed a new plug-in called View Count by Michael O&#8217;Connell. Was pretty simple. Getting the view count was the 1st step in what I was trying to accomplish. I wanted to add a list of the top viewed posts in my sidebar. Installing the plug-in was pretty simple as I just dropped the &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.krunk4ever.com\/blog\/2006\/11\/25\/view-count\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;View Count&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[11],"tags":[],"_links":{"self":[{"href":"https:\/\/www.krunk4ever.com\/blog\/wp-json\/wp\/v2\/posts\/889"}],"collection":[{"href":"https:\/\/www.krunk4ever.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.krunk4ever.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.krunk4ever.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.krunk4ever.com\/blog\/wp-json\/wp\/v2\/comments?post=889"}],"version-history":[{"count":0,"href":"https:\/\/www.krunk4ever.com\/blog\/wp-json\/wp\/v2\/posts\/889\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.krunk4ever.com\/blog\/wp-json\/wp\/v2\/media?parent=889"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.krunk4ever.com\/blog\/wp-json\/wp\/v2\/categories?post=889"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.krunk4ever.com\/blog\/wp-json\/wp\/v2\/tags?post=889"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}