I'm having error in shortcode that "Trying to get property of non-object" on line no 130

Here is code:

while ( $our_work->have_posts() ) { $our_work->the_post();

        $termsArray = get_the_terms( $post->ID, "filter" );  //This is the line I'm getting error. which is line no 130.

        $termsString = ""; //initialize the string that will contain the terms

        foreach ( $termsArray as $term ) { // for each term 
            $termsString .= $term->slug.' '; //create a string that has all the slugs 

        ........(content is here)

filter is custom taxonomy. And I'm using isotope jquery plugin which is working fine in page but giving this error in shortcode. I dont know why this is giving error. Please help.Thanks.

1 Answer 1


You need to call global $post before using $post->ID, or better, use get_the_ID() instead.

  • He's already in the loop, I don't think there's a need to declare it as a global. Commented Mar 11, 2015 at 19:07
  • @cristian.raiber if you're inside a shortcode function, there is no $post var local to that function.
    – Milo
    Commented Mar 11, 2015 at 19:13
  • Thank you so much. It worked . You are genius. Thanks again.
    – Hussy571
    Commented Mar 11, 2015 at 19:26
  • wasn't aware of that @Milo. Oh well, learned a new thing today :) Later edit: oh yeah, makes total sense, since it's a shortcode (dooh); wasn't thinking straight. Commented Mar 11, 2015 at 20:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.