Working in Wordpress I wanted to check the type of page. I.e. is the content type as page or post. I used the global $post Wordpress variable and when I did variable dump for the object using print_r.
I got
So when I tried using as $post['post_type'] I got the following error.
Fatal error: Cannot use object of type stdClass as array
Searching online I found lot of solutions that talk about Upgrading PHP as well as doing ini settings change. I was sure that I am not going to do anything of that sort and finally I found that $post is an object and not an array and so you should access it like
and not
I hope this helps lot of fellow Wordpress Developer's
Code:
echo '<pre>';print_r($post);echo'</pre>';
Code:
[post_title] => Hello world!
[post_excerpt] => Small Description about the post.
[post_status] => publish
[comment_status] => open
[ping_status] => open
[post_password] =>
[post_name] => hello-world
[to_ping] =>
[pinged] =>
[post_modified] => 2011-03-12 13:57:23
[post_modified_gmt] => 2011-03-12 13:57:23
[post_content_filtered] =>
[post_parent] => 0
[guid] => Post URI
[menu_order] => 0
[post_type] => post
[post_mime_type] =>
[comment_count] => 0
[ancestors] => Array
(
)
[filter] => raw
Fatal error: Cannot use object of type stdClass as array
Searching online I found lot of solutions that talk about Upgrading PHP as well as doing ini settings change. I was sure that I am not going to do anything of that sort and finally I found that $post is an object and not an array and so you should access it like
PHP Code:
$post->post_type
PHP Code:
$post['post_type']
