Error: Attempt to read property "ID" on null

I have the following error Attempt to read property ID on null in this code snippet

global $wp_query;
$categories =  get_the_terms($wp_query-post-ID, 'live_stream_categories');
$cat_slug = '';
$live_stream_true = False;
if(!empty($categories)){
    foreach ($categories as $cat) {
        if($cat-slug != 'live-stream')
        {
            $cat_slug = $cat-slug;
        }
        if($cat-slug == 'live-stream')
        {
            $live_stream_true = True;
        }
     }
 }

However I'm not sure what the workaround would be. Any advice? I've took over this site from a previous developer

Topic warnings wp-query errors Wordpress

Category Web


Where did you put that function ? that's better call that it inside the action, the error probably coming from missing variable.

add_action('init','newFunction');
function newFunction(){
 global $wp_query;
 // Do your code here
}

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.