0

I am trying to create a function for count post views. for this i have added code in my theme function file.

Here is my code:

function processpost_views( ){
        global $user_ID, $post;
        if( is_int( $post ) ) {
            $post = get_post( $post );
        }
    if( !wp_is_post_revision( $post ) )
    {
        if( is_single() || is_page() )
        {
            $id = intval( $post->ID );
            global $post;

            $date = explode('-', date('W-d-m-Y', current_time('timestamp')));
            $array=     array(
            0 => $date[3].$date[2].$date[1],    // day like 20140324
            1 => $date[3].$date[0],             // week like 201439
            2 => $date[3].$date[2],             // month like 201405
            3 => $date[3],                      // year like 2014
            4 => 'total'                        // total views
           );
           foreach ($array as $type => $period)
           {
              insertView($post->ID, $type, $period, 1);
           }
        }
    }

}

second function

function insertView( $id, $type, $period, $count = 1 ) {

    global $wpdb;
        $wpdb->postviews = "{$wpdb->prefix}postviews";
        $wpdb->test = "{$wpdb->prefix}test";
        $count = (int) $count;      

        if ( ! $count ) {
            $count = 1;
        }
        $sql = $wpdb->prepare("
                INSERT INTO " . $wpdb->prefix . "postviews (id, type, period, count)
                VALUES (%d, %d, %s, %d)
                ON DUPLICATE KEY UPDATE count = count + %d",
                $id,
                $type,
                $period,
                $count,
                $count
            );      

        $wpdb->query($sql);


}

add_action( 'wp_head', 'processpost_views' );

Sql table

CREATE TABLE IF NOT EXISTS `wp_postviews` (
`id` bigint(20) unsigned NOT NULL,
`type` tinyint(1) unsigned NOT NULL,
`period` varchar(8) NOT NULL,
`count` bigint(20) unsigned NOT NULL,
PRIMARY KEY (`type`,`period`,`id`),
UNIQUE KEY `id_period` (`id`,`period`) USING BTREE,
KEY `type_period_count` (`type`,`period`,`count`) USING BTREE
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

In this above code, my insert and update queries work i am logged in but when i am guest/ logged user this code not work. Please help me.

5
  • Why ENGINE=MyISAM?
    – fuxia
    Apr 20, 2015 at 10:58
  • No any specific reason behind myisam
    – Addy
    Apr 20, 2015 at 11:00
  • My script works fine when i am logged in user but script not insert data when i am logged our
    – Addy
    Apr 20, 2015 at 11:01
  • I am also using : global $wpdb; $wpdb->postviews = "{$wpdb->prefix}postviews"; for set table in wpdb global object
    – Addy
    Apr 20, 2015 at 11:03
  • I tried your code and its working in both case logged in and non-logged in. Apr 20, 2015 at 11:06

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.