getPost($id|WP_Post, $fields)

Get meta information or specific fields from a post, ACF fields included.

Usage

site()->getPost($idOrPost, $fields);

Parameters

ParameterTypeRequiredDescription
$idOrPostint|WP_PostYesA post id or WP_Post object
$fieldsarrayNoPost fields

Returns

TypeDescription
arrayIf fields were provided, values will be returned under those keys. Otherwise, only a post (WP_Post) and meta (array) field will be returned.

Example

Using getPost to fetch information about a post with the id 96.

With Fields

site()->getPost(96, [
'id',
'url',
'title',
'acf.miles',
'taxonomy.car_type.slug,name'
]);
 
// Returns
[
"id" => 96,
"url" => "http://used.car.site/vehicles/2019-volkswagen-gti-4-door/",
"title" => "2019 Volkswagen GTI 4-Door",
"miles" => 3148
"car_type" => [
[
"slug" => "hatchback",
"name" => "Hatchback"
],
[
"slug" => "sportscar",
"name" => "Sports Car"
]
]
]
Grabbing various attributes of a single post

Without Fields

site()->getPost(96);
 
// Returns
[
"post" => object(WP_Post),
"meta" => [
"_edit_last" => [1],
"_edit_lock" => ["1626473973:1"]
... // And many more
]
]
Getting the entire WP_Post object