WordPress.org

Ready to get started?Download WordPress

Forums

WP-PostViews
postviews_id is an Undefined Index (4 posts)

  1. Dorival
    Member
    Posted 2 years ago #

    The following error happens when the plugin is activated:

    Notice: Undefined index: postviews_id in /mounted-storage/path-to-my-wp/wp-content/plugins/wp-postviews/wp-postviews.php on line 648

    I tried to fix it but I made it even worst, so I decided to raise this topic. The following piece of code is where the error happens, in acord of the error above:

    ### Function: Increment Post Views
    increment_views();
    function increment_views() {
    	global $wpdb;
    	$post_id = intval($_GET['postviews_id']);
    	if($post_id > 0 && defined('WP_CACHE') && WP_CACHE) {
    		$post_views = get_post_custom($post_id);
    		$post_views = intval($post_views['views'][0]);
    		if(!update_post_meta($post_id, 'views', ($post_views+1))) {
    			add_post_meta($post_id, 'views', 1, true);
    		}
    	}
    }
  2. Lester Chan
    Member
    Plugin Author

    Posted 2 years ago #

    notices are harmless but it is a bad practice for my part to code with notices off. Google how to off notices in php

  3. Rahe
    Member
    Posted 2 years ago #

    Hi !
    I have the same issue because I develop with WP_DEBUG at true, you can fix this like this :

    function increment_views() {
    	global $wpdb;
    	if( !isset($_GET['postviews_id'] ) )
    		return false;
    	$post_id = intval($_GET['postviews_id']);
    	if($post_id > 0 && defined('WP_CACHE') && WP_CACHE) {
    		$post_views = get_post_custom($post_id);
    		$post_views = intval($post_views['views'][0]);
    		if(!update_post_meta($post_id, 'views', ($post_views+1))) {
    			add_post_meta($post_id, 'views', 1, true);
    		}
    	}
    }

    Seems to work and not blocking the plugin :)

  4. cbee
    Member
    Posted 1 year ago #

    can someone help me?

    I was updating plugins and something happened. My website and backend have these notices such as

    Notice: Undefined index: path in /data/xx/user/xx/htdocs/wp-content/plugins/custom-permalinks/

    a) how do I fix the code
    b) is it possible for all of these notices to be removed by turning off debug error reporting in wp-config?

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic