Reputation: 10221
I am in trouble with a simple task, given a post retrieved in this manner:
$firstRowPost = PostService::GetLastPostByCategory('video');
var_dump($firstRowPost); // this show me the post, and also the ID
var_dump($firstRowPost->ID); // the ID is null :(
class PostService {
public static function GetLastPostByCategory($categoryId) {
$args = array(
'posts_per_page' => 1,
'cat' => $categoryId
);
$latestPost = get_posts($args);
return $latestPost;
}
}
I can get the entire post, but not the post ID
C:\xampp\apps\wordpress\htdocs\wp-content\themes\chester-app\mvc\controllers\index_controller.php:10:
array (size=1)
0 =>
object(WP_Post)[259]
public 'ID' => int 52
public 'post_author' => string '1' (length=1)
public 'post_date' => string '2017-10-06 10:31:40' (length=19)
public 'post_date_gmt' => string '2017-10-06 10:31:40' (length=19)
public 'post_content' => string '<img src="http://localhost/wordpress/wp-content/uploads/2017/10/Cattura.png" alt="" width="1061" height="370" class="alignnone size-full wp-image-37" />
example post video' (length=172)
public 'post_title' => string 'example post video' (length=18)
public 'post_excerpt' => string '' (length=0)
public 'post_status' => string 'publish' (length=7)
public 'comment_status' => string 'open' (length=4)
public 'ping_status' => string 'open' (length=4)
public 'post_password' => string '' (length=0)
public 'post_name' => string 'example-post-video' (length=18)
public 'to_ping' => string '' (length=0)
public 'pinged' => string '' (length=0)
public 'post_modified' => string '2017-10-06 12:27:34' (length=19)
public 'post_modified_gmt' => string '2017-10-06 12:27:34' (length=19)
public 'post_content_filtered' => string '' (length=0)
public 'post_parent' => int 0
public 'guid' => string 'http://localhost/wordpress/?p=52' (length=32)
public 'menu_order' => int 0
public 'post_type' => string 'post' (length=4)
public 'post_mime_type' => string '' (length=0)
public 'comment_count' => string '0' (length=1)
public 'filter' => string 'raw' (length=3)
I don't understand why I can't get the post ID,
C:\xampp\apps\wordpress\htdocs\wp-content\themes\chester-app\mvc\controllers\index_controller.php:11:null
Any help to understand why? Thanks in advance
Upvotes: 1
Views: 52
Reputation: 8171
As your data is actually an array of objects, the correct syntax should be:
$firstRowPost[0]->ID
Upvotes: 1