CyberShot Posted September 28, 2017 Share Posted September 28, 2017 I am working in WordPress and I have always had trouble figuring out what I am working with. For example, I have a foreach loop that when I dump the variable, gives me this foreach( $prevPost as $catPost){ var_dump($catPost); //result of dump object(WP_Post)#1074 (24) { ["ID"]=> int(939) } } So, I then thing that in order to get the id field, I need to do $catPost->ID; only when I do that, I get an error saying "Trying to get property of non-object. My confusion comes in where it says Object(WP_Post). Why is this not an object? How would I need to go about getting that ID? when I try to get the ID by doing this Quote Link to comment https://forums.phpfreaks.com/topic/305135-why-isnt-this-an-object/ Share on other sites More sharing options...
requinix Posted September 28, 2017 Share Posted September 28, 2017 $catPost->ID is only guaranteed to work if you put it in the same place as the var_dump(). Anywhere else and you have to consider what happened to the variable. What is the rest of the code between the foreach and the place where you're trying to use the ID? Quote Link to comment https://forums.phpfreaks.com/topic/305135-why-isnt-this-an-object/#findComment-1552017 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.