PHPackages                             arraypress/wp-post-utils - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. arraypress/wp-post-utils

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

arraypress/wp-post-utils
========================

A lean WordPress library for working with posts and post operations

10PHP

Since Jul 13Pushed 10mo agoCompare

[ Source](https://github.com/arraypress/wp-post-utils)[ Packagist](https://packagist.org/packages/arraypress/wp-post-utils)[ RSS](/packages/arraypress-wp-post-utils/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

WordPress Post Utilities
========================

[](#wordpress-post-utilities)

A lightweight WordPress library for working with posts and post operations. Provides clean APIs for post retrieval, content management, meta operations, and bulk actions with value/label formatting perfect for forms and admin interfaces.

Features
--------

[](#features)

- 🎯 **Clean API**: WordPress-style snake\_case methods with consistent interfaces
- 🔍 **Built-in Search**: Post search with value/label formatting for forms
- 📋 **Form-Ready Options**: Perfect value/label arrays for selects and admin interfaces
- 🔗 **Term Management**: Easy term assignment and relationship management
- 📊 **Meta Operations**: Simple post meta handling with type safety
- 🎨 **Flexible Identifiers**: Use IDs, slugs, titles, or objects interchangeably
- ⚡ **Bulk Operations**: Efficient bulk status changes, deletions, and updates
- ➕ **Post Creation**: Create single or multiple posts with flexible options

Requirements
------------

[](#requirements)

- PHP 7.4 or later
- WordPress 5.0 or later

Installation
------------

[](#installation)

```
composer require arraypress/wp-post-utils
```

Basic Usage
-----------

[](#basic-usage)

### Working with Single Posts

[](#working-with-single-posts)

```
use ArrayPress\PostUtils\Post;

// Get post by ID
$post = Post::get( 123 );

// Get post by identifier (ID, slug, or title)
$post = Post::get_by_identifier( 'hello-world' );
$post = Post::get_by_slug( 'hello-world' );
$post = Post::get_by_title( 'Hello World' );

// Check if post exists
if ( Post::exists( 123 ) ) {
	// Post exists
}

// Create a new post
$post = Post::create( 'My New Post', 'This is the content' );

// Create post with additional data
$post = Post::create( 'My New Post', 'Content here', [
	'post_type'   => 'page',
	'post_status' => 'draft',
	'post_author' => 5,
	'meta_input'  => [
		'custom_field' => 'value'
	]
] );

// Create post only if it doesn't exist
$post = Post::create_if_not_exists( 'Unique Title', 'Content' );

// Get post content and meta
$title   = Post::get_title( 123 );
$content = Post::get_content( 123 );
$excerpt = Post::get_excerpt( 123 );
$url     = Post::get_url( 123 );
$slug    = Post::get_slug( 123 );
$type    = Post::get_type( 123 );

// Get author information
$author_id = Post::get_author_id( 123 );
$author    = Post::get_author( 123 ); // Returns WP_User object

// Get post meta
$meta_value        = Post::get_meta( 123, 'custom_field' );
$meta_with_default = Post::get_meta_with_default( 123, 'priority', 'normal' );

// Check post status and conditions
if ( Post::is_published( 123 ) ) {
	// Post is published
}

if ( Post::is_draft( 123 ) ) {
	// Post is draft
}

if ( Post::is_scheduled( 123 ) ) {
	// Post is scheduled
}

if ( Post::is_type( 123, 'page' ) ) {
	// Post is a page
}

if ( Post::is_type( 123, [ 'post', 'custom_post' ] ) ) {
	// Post is either a post or custom_post
}

if ( Post::has_thumbnail( 123 ) ) {
	$thumbnail = Post::get_thumbnail_url( 123 );
}

if ( Post::has_content( 123 ) ) {
	// Post has content
}

if ( Post::has_excerpt( 123 ) ) {
	// Post has manual excerpt
}

if ( Post::is_sticky( 123 ) ) {
	// Post is sticky
}

if ( Post::allows_comments( 123 ) ) {
	$comment_count = Post::get_comment_count( 123 );
}

// Content analysis
$words        = Post::count_words( 123 );
$reading_time = Post::get_reading_time( 123 ); // minutes
$age_days     = Post::get_age( 123 );

// Post management
Post::update_status( 123, 'draft' );
Post::trash( 123 );
Post::delete( 123, true ); // force delete

// Dates
$date     = Post::get_date( 123, 'Y-m-d' );
$modified = Post::get_modified_date( 123 );
```

### Working with Multiple Posts

[](#working-with-multiple-posts)

```
