wp query - Error: Attempt to read property "ID" on null

admin2025-01-08  4

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

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

Share Improve this question asked Apr 21, 2022 at 8:15 pee2peepee2pee 1411 silver badge13 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 0

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
}

To fix that error (on php8 & over) try this:

global $wp_query;
$post_id = get_queried_object_id();
$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;
        }
     }
 }

Issue coming if code is executed outside of the main WordPress loop or if there are no posts found by the query.

you can first check if $wp_query->post is not null before attempting to access its properties. Additionally, it's good practice to check if $wp_query itself is not null to avoid potential errors

global $wp_query;
if ($wp_query && $wp_query->post) {
    $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;
            }
        }
    }
} else {
    // Handle the case where there is no post available
    // For example, you might set default values or display an error message
    $cat_slug = '';
    $live_stream_true = false;
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736266917a1160.html

最新回复(0)