Wordpress creating plugin for most viewed posts problem?

Posted by user303832 on Stack Overflow See other posts from Stack Overflow or by user303832
Published on 2010-06-18T05:30:12Z Indexed on 2010/06/18 5:33 UTC
Read the original article Hit count: 302

Hello,I just want to create plugin that will when visitor(user,visitor,...) visit some post,remember what post,and to increment counter of that post,I wrote this code,but sometimes,counter is incremented,even post isn't viewed,or post with other Id is added to a table.Can someone help me with this,please.I know that there are plugins for this that I'm trying to do,but still want to write this plugin.

function IncrementPostCount($the_content) {
    global $post;
    global $wpdb;

    if(($post->post_status == 'publish') && (int)$post->ID) {

        if(is_single()) { // just for single post - not for page
            $postID = (int)$post->ID;

            $postTitle = urlencode($post->post_title);
            $postLink = urlencode(get_permalink($post->ID));

            $oneRow = $wpdb->get_row("SELECT * FROM wp_postovi WHERE postAjDi='$postID'");

            if(empty ($oneRow)) {
                $postCounter = 1;
                $data_array = array(
                   'readnTimes' => $postCounter, 
                   'linkPost'=>$postLink, 
                   'TitlePost'=>$postTitle,
                   'postAjDi'=>$postID);
                $wpdb->insert('wp_najcitaniji_postovi', $data_array);                
            }
            else {
                $postCounter = intval($oneRow->readnTimes) + 1;
                $data_array = array('readnTimes' => $postCounter);
                $where_array = array('postAjDi'=>intval($oneRow->postAjDi));
                $wpdb->update('wp_postovi',$data_array,$where_array);
            }

            return $the_content;
        }
        return $the_content;
    }
}
add_filter('the_content','IncrementPostCount');

Sorry on my bad english,tnx in advance.

© Stack Overflow or respective owner

Related posts about Wordpress

Related posts about wordpress-plugin