Jump to content

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
https://forums.phpfreaks.com/topic/305135-why-isnt-this-an-object/
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?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.