Support » Plugin: Counterize » $post_id not set if displaying 404 page

  • If a 404 error is thrown the the plugin writes the line as shown below in the debug.log.

    PHP Notice: Trying to get property of non-object in D:\www\wordpress_new\wp-content\plugins\counterize\counterize.php on line 1722

    error occurs because no post_id is found:

    $post_id = ( ( is_single() || $post->post_type == 'page' ) ? $post->ID : 'NULL' );

    I fixed this by adding isset to the code like this:

    //search for an eventual post ID
    if (isset( $post_id ) ) {
    $post_id = ( ( is_single() || $post->post_type == 'page' ) ? $post->ID : 'NULL' );
    	if( $post_id == 'NULL' )
    	{
    		$post_id = url_to_postid( $url );
    		if( $post_id == 0 )
    		{
    			$post_id = 'NULL';
    		}
    	}
    else { $post_id = 'NULL'; }
    		}

    https://wordpress.org/plugins/counterize/

  • The topic ‘$post_id not set if displaying 404 page’ is closed to new replies.