You are getting the error because, by default, wp_get_recent_posts
returns an array of posts (due to backward compatibilty) and not the required WP_Post
object which is required to setup the template tags.
Look at the source code, and you will see that wp_get_recent_posts
is a simple wrapper function for get_posts
(which is just a wrapper for WP_Query
). What makes the difference in output from wp_get_recent_posts
, is the value of the it’s second parameter, $output
which is by default set to ARRAY_A
. By default, the output from get_posts
is converted to an array. Here is the piece of code responsible for that.
// Backward compatibility. Prior to 3.1 expected posts to be returned in array.
if ( ARRAY_A == $output ){
foreach( $results as $key => $result ) {
$results[$key] = get_object_vars( $result );
}
return $results ? $results : array();
}
To make your code work, simply pass an empty string (or any crap for that matter) to the second parameter of wp_get_recent_posts
which will return the WP_Post
object directly from get_posts
$featured_post = wp_get_recent_posts(array( 'numberposts'=>'1', 'post_type' => 'post' ), '');
or, for that matter
$featured_post = wp_get_recent_posts(array( 'numberposts'=>'1', 'post_type' => 'post' ), 'CRAP');
On final thing, simply use the_title()
to display the title, no need for echo get_the_title()
EDIT
Don’t forget to call wp_reset_postdata()
after your foreach
loop