AbstractPost

Abstract class.

use Saij\WpAppFramework\Post\AbstractPost;

The object of a post: a class with #[PostType] extends it, and the framework provides Post and Page for the post types of WordPress.

A post object is loaded with Event::get($id) or Event::find($id) and created with new Event(). It holds the data of the post (title, content, status, …) and its meta objects (meta(EventDetails::class)); save() checks every meta value against its schema and writes the post and the changed meta values.

Every get() and find() returns a new object; changes are visible to others only after save(). save() checks no capabilities, like wp_update_post().

Methods

find()

AbstractPost::find(int $id): ?static

The post with the ID, in any status; null if it does not exist or has another post type.

ParameterTypeDefaultDescription
idintrequiredThe ID of the post.

Returns ?AbstractPost

get()

AbstractPost::get(int $id): static

The post with the ID, in any status.

ParameterTypeDefaultDescription
idintrequiredThe ID of the post.

Returns AbstractPost

Throws InvalidArgumentException: If it does not exist or has another post type.

query()

AbstractPost::query(): PostQuery

A query for posts of the type: published posts, unsorted conditions, see PostQuery.

Returns PostQuery<AbstractPost>

delete()

delete(bool $force = false): void

Moves the post to the trash, or deletes it permanently with $force. WordPress moves only posts and pages to the trash (and only while the trash is enabled, EMPTY_TRASH_DAYS); posts of other types and posts already in the trash are deleted permanently either way.

ParameterTypeDefaultDescription
forceboolfalseDelete permanently instead of moving to the trash.

Throws LogicException: If the post has not been saved.

Throws RuntimeException: If WordPress cannot delete the post.

getAuthorId()

getAuthorId(): ?int

The ID of the author, null without author.

Returns ?int

getContent()

getContent(): string

The content as stored: block markup.

Returns string

getDate()

getDate(): ?DateTimeImmutable

The publication date in UTC, null for posts without one (drafts).

Returns ?DateTimeImmutable

getExcerpt()

getExcerpt(): string

The manual excerpt as stored; empty without one.

Returns string

getId()

getId(): ?int

The ID, null before the post has been saved.

Returns ?int

getModified()

getModified(): ?DateTimeImmutable

The date of the last change in UTC, null before the post has been saved.

Returns ?DateTimeImmutable

getParent()

getParent(): ?static

The parent post (hierarchical post types), null without parent.

Returns ?AbstractPost

getSlug()

getSlug(): string

The slug (post_name), empty until WordPress assigns one.

Returns string

getStatus()

getStatus(): PostStatus

The status; a new post is a draft.

Returns PostStatus

getTerms()

getTerms(string $taxonomy): array

The terms of a taxonomy (a class with #[Taxonomy]), as stored; changes by setTerms() count after save().

ParameterTypeDefaultDescription
taxonomyclass-stringrequiredThe taxonomy class: a class with #[Taxonomy], Category or Tag.

Returns list<WP_Term>: The terms; empty before the post has been saved.

getTitle()

getTitle(): string

The title as stored, or as set by setTitle() before save().

Returns string

getUrl()

getUrl(): ?string

The permalink, null before the post has been saved.

Returns ?string

meta()

meta(string $class): object

The meta object of a meta class (#[PostMeta] naming this post class), loaded on first use; the same object until the post object is gone. In templates the meta class is named by its #[PostMeta] name: post.meta('details').

ParameterTypeDefaultDescription
classclass-string<T>|stringrequiredThe meta class, or its #[PostMeta] name.

Returns ($class is class-string<T> ? T : object)

save()

save(): void

Stores the post: checks the loaded meta objects against their schemas (nothing is written if a value is invalid), inserts or updates the post and writes the changed meta values and terms.

Throws PostValidationException: If a meta value is invalid.

Throws RuntimeException: If WordPress cannot save the post.

setAuthorId()

setAuthorId(int $author): void

Sets the author; stored by save().

ParameterTypeDefaultDescription
authorintrequiredThe ID of the user.

setContent()

setContent(string $content): void

Sets the content; stored by save().

ParameterTypeDefaultDescription
contentstringrequiredBlock markup.

setExcerpt()

setExcerpt(string $excerpt): void

Sets the manual excerpt; stored by save().

ParameterTypeDefaultDescription
excerptstringrequiredThe excerpt; empty for none.

setParent()

setParent(?AbstractPost $parent): void

Sets the parent post (hierarchical post types); stored by save().

ParameterTypeDefaultDescription
parentAbstractPost|nullrequiredA saved post of the same class; null for none.

Throws LogicException: If the post type is not hierarchical.

Throws InvalidArgumentException: If the parent has another class or has not been saved.

setSlug()

setSlug(string $slug): void

Sets the slug (post_name); stored by save().

ParameterTypeDefaultDescription
slugstringrequiredThe slug; empty lets WordPress derive it from the title when the post is published.

setStatus()

setStatus(PostStatus $status): void

Sets the status; stored by save(). The capabilities of the current user are not checked.

ParameterTypeDefaultDescription
statusPostStatusrequiredThe new status.

setTerms()

setTerms(string $taxonomy, array $termIds): void

Sets the terms of a taxonomy (a class with #[Taxonomy]); stored by save(), replacing the terms the post had.

ParameterTypeDefaultDescription
taxonomyclass-stringrequiredThe taxonomy class: a class with #[Taxonomy], Category or Tag.
termIdslist<int>requiredThe IDs of the terms; empty to remove all terms of the taxonomy.

setTitle()

setTitle(string $title): void

Sets the title; stored by save().

ParameterTypeDefaultDescription
titlestringrequiredThe title, plain text.

toWpPost()

toWpPost(): ?WP_Post

The WordPress post, null before the post has been saved.

Returns ?WP_Post