PHPackages                             yard/data - 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. yard/data

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

yard/data
=========

Powerful data objects for WordPress

v1.5.1(2mo ago)03.8k↓60.2%[7 issues](https://github.com/yardinternet/wp-data/issues)[5 PRs](https://github.com/yardinternet/wp-data/pulls)2MITPHPPHP &gt;=8.2CI passing

Since Jun 11Pushed 2w ago2 watchersCompare

[ Source](https://github.com/yardinternet/wp-data)[ Packagist](https://packagist.org/packages/yard/data)[ RSS](/packages/yard-data/feed)WikiDiscussions main Synced 2d ago

READMEChangelog (10)Dependencies (24)Versions (31)Used By (2)

WP Data Objects
===============

[](#wp-data-objects)

[![Code Style](https://github.com/yardinternet/wp-data/actions/workflows/format-php.yml/badge.svg?no-cache)](https://github.com/yardinternet/wp-data/actions/workflows/format-php.yml)[![PHPStan](https://github.com/yardinternet/wp-data/actions/workflows/phpstan.yml/badge.svg?no-cache)](https://github.com/yardinternet/wp-data/actions/workflows/phpstan.yml)[![Tests](https://github.com/yardinternet/wp-data/actions/workflows/run-tests.yml/badge.svg?no-cache)](https://github.com/yardinternet/wp-data/actions/workflows/run-tests.yml)[![Code Coverage Badge](https://github.com/yardinternet/wp-data/raw/badges/coverage.svg)](https://github.com/yardinternet/wp-data/actions/workflows/badges.yml)[![Lines of Code Badge](https://github.com/yardinternet/wp-data/raw/badges/lines-of-code.svg)](https://github.com/yardinternet/wp-data/actions/workflows/badges.yml)

Powerful data objects for WordPress.

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

[](#requirements)

- [Sage](https://github.com/roots/sage) &gt;= 10.0
- [Acorn](https://github.com/roots/acorn) &gt;= 3.0

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

[](#installation)

You can install this package with Composer:

```
composer require yard/data
```

You can publish the config file with:

```
wp acorn vendor:publish --provider="Yard\Data\Providers\DataServiceProvider"
```

Usage
-----

[](#usage)

### PostData

[](#postdata)

#### Creating PostData

[](#creating-postdata)

PostData can be created from the global `\WP_Post` object:

```
global $post;
$postData = \Yard\Data\PostData::from($post);
```

From an array:

```
$postData = \Yard\Data\PostData::from(
    [
        'id' => 42,
        'author' => 1,
        'title' => 'Hello, World!',
        'content' => 'This is a test post.',
        'excerpt' => 'This is a test post.',
        'status' => 'publish',
        'date' => '2021-01-01 00:00:00',
        'modified' => '2021-01-01 00:00:00',
        'postType' => 'post',
        'slug'=> 'hello-world',
    ]
);
```

Or from an Eloquent Model using Corcel:

```
$model = \Corcel\Model\Post::find(get_the_ID());
$postData = \Yard\Data\PostData::from($model);
```

### Custom PostData

[](#custom-postdata)

Creating a VacancyData object by extending PostData:

```
namespace App\Data;

use Yard\Data\PostData;

class VacancyData extends PostData
{
}
```

Enables you to create VacancyData object in the same way as PostData:

```
global $post;
$postData = \App\Data\VacancyData::from($post);
```

#### Configuring the returning Instance

[](#configuring-the-returning-instance)

Every time you call `Yard\Data\PostData::from($post)` you receive an instance of `Yard\Data\PostData`.

If you choose to create a new data class for your custom post type, you can have this class be returned for all instances of that post type.

To do this, you need to add the Fully Qualified Class Name (FQCN) of your custom data class to the `supports` array when registering your custom post type:

```
register_post_type('vacancy', [
    'supports' => [
        'data-class' => ['classFQN' => App\Data\KnowledgebaseData::class],
    ],
]);
```

Another option is to create a mapping in the `config/yard-data.php` file. The mapping in the project config takes precedence over the register\_post\_type `supports` args.

```
'post_types' => [
  'vacancy' => App\Data\VacancyData::class,
  'employee' => App\Data\EmployeeData::class,
```

Now every time you call `Yard\Data\PostData::from($post)` on a custom post type the mapped instance will be returned.

### Meta Fields

[](#meta-fields)

#### Using the Meta Attribute

[](#using-the-meta-attribute)

Adding a meta field with a meta\_key of `vacancy_email` to your VacancyData looks like this:

```
namespace App\Data;

use Yard\Data\Attributes\Meta;
use Yard\Data\PostData;

class VacancyData extends PostData
{
    #[Meta(metaKey: 'vacancy_email')]
    public string $email;
}
```

This approach is functionally equivalent to using:

```
#[Meta]
public string $vacancyEmail;
```

You can also specify any available Data Object, and the meta value will be cast to that Data Object:

```
#[Meta]
public EmployeeData $vacancyEmployee;
```

#### The MetaPrefix Class Attribute

[](#the-metaprefix-class-attribute)

If all of your meta fields are prefixed with the same prefix you can use the MetaPrefix attribute:

```
namespace App\Data;

use Yard\Data\Attributes\MetaPrefix;
use Yard\Data\Attributes\Meta;
use Yard\Data\PostData;

#[MetaPrefix(prefix: 'vacancy')]
class VacancyData extends PostData
{
    #[Meta]
    public string $email;
}
```

It doesn’t matter if your meta keys are in snake\_case and your attributes are in camelCase. For instance, let’s say your meta key is `vacancy_members_only`:

```
#[MetaPrefix(prefix: 'vacancy')]
class VacancyData extends PostData
{
    #[Meta]
    public bool $membersOnly;
}
```

### Taxonomy Terms

[](#taxonomy-terms)

#### Adding terms to your Data Object

[](#adding-terms-to-your-data-object)

For every taxonomy that has been registered with your custom post type you can add a Collection of TermData like this:

```
namespace App\Data;

use Illuminate\Support\Collection;
use Yard\Data\Attributes\Terms;
use Yard\Data\PostData;

class VacancyData extends PostData
{
    #[Terms(taxonomy: 'vacancy_location')]
    /** @var Collection */
    public Collection $locations;
}
```

This approach is functionally equivalent to using:

```
#[Terms]
/** @var Collection */
public Collection $vacancyLocation;
```

or:

```
#[Terms]
/** @var Collection */
public Collection $vacancyLocations;
```

#### The TaxonomyPrefix Class Attribute

[](#the-taxonomyprefix-class-attribute)

If all of your taxonomies are prefixed with the same prefix you can use the TaxonomyPrefix attribute:

```
namespace App\Data;

use Illuminate\Support\Collection;
use Yard\Data\Attributes\TaxonomyPrefix;
use Yard\Data\Attributes\Terms;
use Yard\Data\PostData;

#[TaxonomyPrefix(prefix: 'vacancy')]
class VacancyData extends PostData
{
    #[Terms]
    /** @var Collection */
    public Collection $locations;
}
```

#### Reading Terms from your Data Object

[](#reading-terms-from-your-data-object)

Because Terms are a [Collection](https://laravel.com/docs/collections) you can use any of the available collection methods to read the terms from your data object. Here are some common examples:

```
$vacancyData->locations->contains('slug',  'amsterdam'); // true
$postData->locations->firstWhere('slug', 'utrecht')->name; // Utrecht
$vacancyData->locations->implode('name', ', '), // Almere, Amsterdam, Utrecht
```

#### Extending TermData

[](#extending-termdata)

You can add extra meta fields to taxonomies by extending the default TermData object

```
namespace App\Data;

use Yard\Data\TermData;

class TypeTermData extends TermData {

  #[Meta()]
  public string $icon;
}
```

In your PostData object you have to specify the data class used for a specific taxonomy:

```
namespace App\Data;

use Illuminate\Support\Collection;
use Yard\Data\Attributes\TaxonomyPrefix;
use Yard\Data\Attributes\Terms;
use Yard\Data\PostData;

class VacancyData extends PostData
{
    #[Terms(dataClass: TypeTermData::class)]
    /** @var Collection */
    public Collection $type;
}
```

```
$vacancyTypeIcon = $vacancyData->type->first()?->icon;
```

#### Reading comments for your Data Objects

[](#reading-comments-for-your-data-objects)

If the post type of the data object supports comments, you can retrieve them as a collection of `CommentData`

```
$postData->comments()->first()?->authorEmail;
```

#### Extending CommentData

[](#extending-commentdata)

You can add extra meta fields to comments by extending the default CommentData object

```
namespace App\Data;

use Yard\Data\CommentData;

class LinkedInCommmentData extends CommentDataData {

  #[Meta()]
  public string $linkedInUserName;
}
```

### UserData

[](#userdata)

Create UserData from current user:

```
$userData = UserData::from(wp_get_current_user());
```

About us
--------

[](#about-us)

[![banner](https://raw.githubusercontent.com/yardinternet/.github/refs/heads/main/profile/assets/small-banner-github.svg)](https://www.yard.nl/werken-bij/)

###  Health Score

52

—

FairBetter than 96% of packages

Maintenance93

Actively maintained with recent releases

Popularity22

Limited adoption so far

Community20

Small or concentrated contributor base

Maturity64

Established project with proven stability

 Bus Factor2

2 contributors hold 50%+ of commits

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~43 days

Total

17

Last Release

60d ago

PHP version history (4 changes)1.0.0PHP ^8.1

1.0.6PHP 8.1.\*

v1.0.7PHP &gt;=8.1

v1.3.3PHP &gt;=8.2

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/62775?v=4)[Anton Zhuravsky](/maintainers/Yard)[@yard](https://github.com/yard)

---

Top Contributors

[![SimonvanWijhe](https://avatars.githubusercontent.com/u/41121933?v=4)](https://github.com/SimonvanWijhe "SimonvanWijhe (69 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (49 commits)")[![ictbeheer](https://avatars.githubusercontent.com/u/14947039?v=4)](https://github.com/ictbeheer "ictbeheer (26 commits)")[![FreakyWizard](https://avatars.githubusercontent.com/u/114140418?v=4)](https://github.com/FreakyWizard "FreakyWizard (5 commits)")[![projmunoz](https://avatars.githubusercontent.com/u/149622027?v=4)](https://github.com/projmunoz "projmunoz (2 commits)")[![yard-bot](https://avatars.githubusercontent.com/u/66111179?v=4)](https://github.com/yard-bot "yard-bot (2 commits)")[![YvetteNikolov](https://avatars.githubusercontent.com/u/48315669?v=4)](https://github.com/YvetteNikolov "YvetteNikolov (2 commits)")

---

Tags

wordpress

###  Code Quality

TestsPest

Static AnalysisPHPStan

### Embed Badge

![Health badge](/badges/yard-data/health.svg)

```
[![Health](https://phpackages.com/badges/yard-data/health.svg)](https://phpackages.com/packages/yard-data)
```

###  Alternatives

[helgesverre/extractor

AI-Powered Data Extraction for your Laravel application.

22340.0k](/packages/helgesverre-extractor)[mrmarchone/laravel-auto-crud

Laravel Auto CRUD helps you streamline development and save time.

30614.9k2](/packages/mrmarchone-laravel-auto-crud)[crescat-io/saloon-sdk-generator

Simplified SDK Scaffolding for Saloon

13232.4k10](/packages/crescat-io-saloon-sdk-generator)[tarfin-labs/event-machine

Event-driven state machines for Laravel with event sourcing, type-safe context, and full audit trail.

199.4k](/packages/tarfin-labs-event-machine)[tbruckmaier/corcel-acf

Advanced Custom Field (ACF) plugin for Corcel

3022.1k](/packages/tbruckmaier-corcel-acf)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
