getPosts($args, $fields)

Get specific fields or WP_Post objects for multiple posts.

Usage

site()->getPosts($args, $fields);

Parameters

ParameterTypeRequiredDescription
$argsarrayYesAn associative array of options to determine which posts to return. See below for a list of arguments.
$fieldsarrayNoPost fields

Arguments

KeyTypeDefaultDescription
"numberposts"int5Max amount of posts to return
"category"int0ID of category to filter by
"orderby"string"date"Field to order results by
"order"string"DESC"Sort direction
"include"array[]List of post IDs to include
"exclude"array[]List of post IDs to exclude
"meta_key"string-Used with meta_value
"meta_value"string-Grab posts by a meta value
"post_type"string"post"Grab posts by a post type
"type"string-Alias for "post_type"
"suppress_filters"booltrueWhether to suppress filters
"taxonomy.<type>"string-Returns posts with the defined taxonomies. The string is a comma seperated list of values for the taxonomy type.

As well as anything else that can be passed into WP’s get_posts function.

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 for each post.

Example

In this example we get the name, mileage and country for vehicles from France or Italy.

config.php
return [
// ...
"custom-post-types" => [
[
"slug" => "vehicles",
"taxonomies" => ["country"],
]
]
// ...
];
Registering a custom post type and taxonomy
// Where you call your lookup
site()->getPosts([
"type" => "vehicles",
"taxonomy.country" => "france,italy"
], [
"title",
"acf.miles",
"taxonomy.country.name"
]);
// Returns
[
[
"title" => "2010 Ferrari 458 Italia",
"miles" => 13802,
"country" => [
[
"name" => "Italy"
]
]
],
[
"title" => "2019 Renault Alpine A110 Légende GT",
"miles" => 3211,
"country" => [
[
"name" => "France"
]
]
],
[
"title" => "2019 FIAT 124 Spider Abarth",
"miles" => 9304,
"country" => [
[
"name" => "Italy"
]
]
]
...
]
Grabbing posts based on post type and taxonomy, only returning certain fields.