cmplieger
cmplieger

Reputation: 7351

error while loading wordpress post using ID

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.

Upvotes: 1

Views: 84

Answers (3)

Musa
Musa

Reputation: 97682

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"];

Upvotes: 1

csilk
csilk

Reputation: 2932

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;

Upvotes: 2

xlordt
xlordt

Reputation: 521

change all [''] to -> example

$newpost->ID;
$newpost->post_title

wp passes most parameters as objects and not as arrays.

Upvotes: 1

Related Questions