1

I am trying to retrieve the content of a wordpress post using its ID:

$loadpost = url_to_postid($actual_link); 
$newpost = get_post($loadpost);
echo '<article id="post-'.$newpost["ID"].'"><h1>'.$newpost["post_title"].'</h1>'.$newpost["post_content"];

$loadpost returns a valid ID but somehow this expression does not work. IE returns:

Fatal error: Cannot use object of type stdClass as array in /hermes/waloraweb046/b428/moo.snippetspacecom/splittemplate/wp-content/themes/split/index.php on line 24

What does this mean?

Thanks for your help guys.

cmplieger
  • 7,155
  • 15
  • 55
  • 83
  • http://stackoverflow.com/questions/6171699/cannot-use-object-of-type-stdclass-as-array-using-wordpress – anuragbh Sep 10 '12 at 01:17

3 Answers3

2

Because get_post(); by default outputs as an OBJECT

What you want to return is

echo '<article id="post-'.$newpost->ID.'"><h1>'.$newpost->post_title.'</h1>'.$newpost->post_content;
csilk
  • 2,732
  • 3
  • 23
  • 40
1

change all [''] to -> example

$newpost->ID;
$newpost->post_title

wp passes most parameters as objects and not as arrays.

xlordt
  • 521
  • 4
  • 15
1

By default get_post returns an object, pass ARRAY_A as a second parameter for it to return an associative array.

$loadpost = url_to_postid($actual_link); 
$newpost = get_post($loadpost, ARRAY_A);
echo '<article id="post-'.$newpost["ID"].'"><h1>'.$newpost["post_title"].'</h1>'.$newpost["post_content"];
Musa
  • 96,336
  • 17
  • 118
  • 137
  • @SnippetSpace did you call `get_post` with `ARRAY_A` as a second parameter or just copied my code?, if the latter there was a mistake in my code if is corrected now. – Musa Sep 10 '12 at 01:24