Skip to content

Latest commit

 

History

History
79 lines (56 loc) · 1.41 KB

File metadata and controls

79 lines (56 loc) · 1.41 KB

Posts Service

The PostsService allows you to manage WordPress posts. It supports creating, reading, updating, and deleting posts.

Usage

Access via the facade:

$postsService = $wp->posts();

List Posts

Retrieve a paginated collection of posts.

$posts = $wp->posts()->list([
    'per_page' => 10,
    'status'   => 'publish',
    'categories' => [1, 2] // Filter by category IDs
]);

foreach ($posts as $post) {
    echo $post->title->rendered;
}

Get a Single Post

use JooServices\WordPress\Sdk\Exceptions\NotFoundException;

try {
    $post = $wp->posts()->get(123);
    echo $post->content->rendered;
} catch (NotFoundException $e) {
    echo "Post not found.";
}

Create a Post

$post = $wp->posts()->create([
    'title'   => 'New Post Title',
    'content' => 'Post content goes here.',
    'status'  => 'draft',
    'categories' => [5]
]);

Update a Post

$updatedPost = $wp->posts()->update(123, [
    'title' => 'Updated Title'
]);

Delete a Post

By default, deleting a post sends it to the Trash.

$trashedPost = $wp->posts()->delete(123);

To permanently delete a post:

$deletedPost = $wp->posts()->delete(123, force: true);

See Also

  • ContentBuilder: generate complex post content programmatically.
  • Templates: create posts using structured templates.