Support » Developing with WordPress » A lot of warnings from the code, please help

  • Resolved catuyen

    (@catuyen)


    Hello

    A previous developer helped me write this function to get the post view from REHUB theme and display it in another plugin as a shortcode. It works but when I turn on debug, there are a lot of warnings.

    Code

    function custom_get_view_post( $attrs, $content ) {
    	if(rehub_option('post_view_disable') != 1 && function_exists('RH_get_post_views')){
    	    $post_id = get_the_ID();
    	    $views = intval(RH_get_post_views($post->ID));
    	    return $views;
    	}else{
    		return "";
    	}
     }
    
    // add template tag for w4 post list
    add_filter( 'w4pl/get_shortcodes', 'custom_product_shortcodes', 20 );
    function custom_product_shortcodes( $shortcodes ) {
        $shortcodes['views'] = [
            'group' 	=> 'Post', 
    		'callback' 	=> 'custom_get_view_post',
    		'desc' 		=> '<strong>'. __('Output', 'text-domain') .'</strong>'
        ];
    
        return $shortcodes;
    }
    

    Warnings

    Warning: Undefined variable $post in /home/mysites/web/mydomain.com/public_html/wp-content/themes/rehub-blankchild/functions.php on line 26

    Warning: Attempt to read property “ID” on null in /home/mysites/web/mydomain.com/public_html/wp-content/themes/rehub-blankchild/functions.php on line 26

    Could some expert here please help me correct this code??

    Thank you so much!
    CU

    • This topic was modified 3 months ago by catuyen.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Hardik Thakkar

    (@thakkarhardik)

    Hello @catuyen ,

    Looks like $post is not defined. But you have the post id from $post_id so can you please replace this line

    $views = intval(RH_get_post_views($post->ID));

    with

    $views = intval(RH_get_post_views($post_id));

    and see if that works?

    Thread Starter catuyen

    (@catuyen)

    Amazing @thakkarhardik

    It worked and the warnings are gone.

    God bless you 😉

    • This reply was modified 3 months ago by catuyen.
    Hardik Thakkar

    (@thakkarhardik)

    Thanks @catuyen. Glad it worked.

Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.