Jump to content

why isn't this an object


CyberShot

Recommended Posts

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

Link to comment
Share on other sites

$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?

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.