Core Theme Components

PostsResultSet implements ResultSet

Provides a fluent interface for fetching data from WP.

For example, to retrieve the 10 latest posts of a given Model class:

Model::objects()->filter(['posts_per_page' => 10])->orderBy('post_date')->desc()->get();

or even more concise:

Model::objects()->latest(10)->get();
Tags
final

Interfaces, Classes and Traits

ResultSet
ResultSets provide a fluent interface to fetching data.

Table of Contents

$cacheInSeconds  : int
$data  : array<string|int, mixed>
$immutables  : array<string|int, mixed>
$postClass  : string
$queryParams  : array<string|int, mixed>
__get()  : mixed
__toString()  : string
addToCollection()  : void
Add the results directly to a collection. This is useful for when you are working with an existing collection and wish to merge the results in. Note that the interface for a collection uses Doctrine\Common\Collections\Collection. Collection is not yet part of the standard library: https://www.php.net/manual/en/class.ds-collection.php
all()  : array<string|int, mixed>
Returns all records in the collection. This method will hit the database.
any()  : self
Retrieve results with any status.
asc()  : self
Set ordering to ASC.
cache()  : self
Sets the cache expires time in seconds. -1 is no cache, 0 is cache forever.
cacheForever()  : self
Sets the cache to store forever.
count()  : int
The number of records that will be returned with the query parameters.
dateRange()  : self
Filter by date range. The default is to use 'inclusive' dates.
desc()  : self
Set ordering to DESC.
exclude()  : self
Exclude by ID or parent ID.
exists()  : bool
Check if a post exists. This method will hit the database by performing a `SELECT COUNT(*)...` query
factory()  : self
Creates a ResultSet instance.
filter()  : self
Filter a query by keyword args. See WP_Query documentation for a full list of args. You do not need to pass the 'post_type' parameter as this will automatically be done based on the model class mappings in TimberManager::classMap().
first()  : array<string|int, mixed>
Retrieve only the first result. This method will hit the database.
get()  : array<string|int, mixed>
Returns the result set array based on the set query params. This method will hit the database.
id()  : array<string|int, mixed>
Alias for pk(int $pk), but also accepts an array of IDs. Deprecated, use pk() or include().
ids()  : self
Deprectated, use idsOnly() instead.
idsOnly()  : self
Sets the `fields` parameter to `ids`.
ignoreStickyPosts()  : self
Set `ignore_sticky_posts` to true.
include()  : self
Fetch results by a list of post IDs (or parent post ID).
latest()  : self
Fetch the latest entries by `post_date`.
limit()  : self
Set the `posts_per_page` field.
nocache()  : self
Sets cache to never cache. Same as cache(-1).
order()  : self
Sets the `order` parameter.
orderBy()  : self
Sets the `orderby` value.
page()  : self
Sets the `paged` parameter and sets `no_found_rows` to false.
pk()  : null|object
Returns the first result by primary key (post ID). This method will hit the database.
shuffle()  : array<string|int, mixed>
Shuffles (and slices) the result set. This method will hit the database.
slice()  : array<string|int, mixed>
Returns a slice of the collection starting at the given index.
__construct()  : mixed
getPostTypeValue()  : string
Get the post type mapping value. Defaults to 'post' if mapping not found.

Properties

Methods

__get()

public __get(string $key) : mixed
Parameters
$key : string
Return values
mixed

__toString()

public __toString() : string
Return values
string

addToCollection()

Add the results directly to a collection. This is useful for when you are working with an existing collection and wish to merge the results in. Note that the interface for a collection uses Doctrine\Common\Collections\Collection. Collection is not yet part of the standard library: https://www.php.net/manual/en/class.ds-collection.php

public addToCollection(Collection &$collection[, string $key = null ]) : void
Parameters
$collection : Collection
$key : string = null

Optional key

Return values
void

all()

Returns all records in the collection. This method will hit the database.

public all() : array<string|int, mixed>
Return values
array<string|int, mixed>

any()

Retrieve results with any status.

public any() : self
Return values
self

asc()

Set ordering to ASC.

public asc() : self
Return values
self

cache()

Sets the cache expires time in seconds. -1 is no cache, 0 is cache forever.

public cache(int $seconds) : self
Parameters
$seconds : int
Return values
self

cacheForever()

Sets the cache to store forever.

public cacheForever() : self
Return values
self

count()

The number of records that will be returned with the query parameters.

public count() : int
Return values
int

dateRange()

Filter by date range. The default is to use 'inclusive' dates.

public dateRange(Carbon $start[, Carbon|null $end = null ][, bool $inclusive = true ]) : self
Parameters
$start : Carbon
$end : Carbon|null = null

Optional

$inclusive : bool = true

Optional, default is true.

Return values
self

desc()

Set ordering to DESC.

public desc() : self
Return values
self

exclude()

Exclude by ID or parent ID.

public exclude(array<string|int, mixed> $ids[, bool $parent = false ]) : self
Parameters
$ids : array<string|int, mixed>
$parent : bool = false

If true, excludes by parent ID(s).

Return values
self

exists()

Check if a post exists. This method will hit the database by performing a `SELECT COUNT(*)...` query

public exists(int $pk) : bool
Parameters
$pk : int
Return values
bool

factory()

Creates a ResultSet instance.

public static factory([string $postClass = '' ][, array<string|int, mixed> $params = [] ]) : self
Parameters
$postClass : string = ''
$params : array<string|int, mixed> = []
Return values
self

filter()

Filter a query by keyword args. See WP_Query documentation for a full list of args. You do not need to pass the 'post_type' parameter as this will automatically be done based on the model class mappings in TimberManager::classMap().

public filter(array<string|int, mixed> $params) : self
Parameters
$params : array<string|int, mixed>
Tags
see
TimberManager
Return values
self

first()

Retrieve only the first result. This method will hit the database.

public first() : array<string|int, mixed>
Tags
category

Database Read

Return values
array<string|int, mixed>

get()

Returns the result set array based on the set query params. This method will hit the database.

public get() : array<string|int, mixed>
Tags
category

Database Read

Return values
array<string|int, mixed>

id()

Alias for pk(int $pk), but also accepts an array of IDs. Deprecated, use pk() or include().

public id(array<string|int, mixed>|int $pid) : array<string|int, mixed>
Parameters
$pid : array<string|int, mixed>|int

The post ID(s).

Tags
deprecated
Return values
array<string|int, mixed>

ids()

Deprectated, use idsOnly() instead.

public ids() : self
Tags
deprecated
1.0.1
Return values
self

idsOnly()

Sets the `fields` parameter to `ids`.

public idsOnly() : self
Return values
self

ignoreStickyPosts()

Set `ignore_sticky_posts` to true.

public ignoreStickyPosts() : self
Return values
self

include()

Fetch results by a list of post IDs (or parent post ID).

public include(array<string|int, mixed> $ids[, bool $parent = false ]) : self
Parameters
$ids : array<string|int, mixed>

A list of post IDs to include.

$parent : bool = false

Optional

Return values
self

latest()

Fetch the latest entries by `post_date`.

public latest(int $limit[, bool $ignoreStickyPosts = false ]) : self
Parameters
$limit : int

Option, default is posts_per_page setting value.

$ignoreStickyPosts : bool = false

Optional, default is false.

Return values
self

limit()

Set the `posts_per_page` field.

public limit(int $limit) : self
Parameters
$limit : int
Return values
self

nocache()

Sets cache to never cache. Same as cache(-1).

public nocache() : self
Return values
self

order()

Sets the `order` parameter.

public order([string $order = 'DESC' ]) : self
Parameters
$order : string = 'DESC'
Return values
self

orderBy()

Sets the `orderby` value.

public orderBy(string $by) : self
Parameters
$by : string
Return values
self

page()

Sets the `paged` parameter and sets `no_found_rows` to false.

public page(int $num) : self
Parameters
$num : int
Return values
self

pk()

Returns the first result by primary key (post ID). This method will hit the database.

public pk(int $pk) : null|object
Parameters
$pk : int
Tags
throws
UnexpectedValueException
Return values
null|object

shuffle()

Shuffles (and slices) the result set. This method will hit the database.

public shuffle(int $andSlice) : array<string|int, mixed>
Parameters
$andSlice : int

Optional

Tags
category

Database Read

Return values
array<string|int, mixed>

slice()

Returns a slice of the collection starting at the given index.

public slice(int $start) : array<string|int, mixed>

Similar to Laravel's slice(). This method will hit the database.

Parameters
$start : int
Tags
category

Database Read

Return values
array<string|int, mixed>

__construct()

private __construct([string $postClass = '' ][, array<string|int, mixed> $queryParams = [] ]) : mixed
Parameters
$postClass : string = ''
$queryParams : array<string|int, mixed> = []
Return values
mixed

getPostTypeValue()

Get the post type mapping value. Defaults to 'post' if mapping not found.

private getPostTypeValue(string $postClass) : string
Parameters
$postClass : string
Return values
string

Search results