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

ActivePackage

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

Powerful data objects for WordPress

v1.4.0(2mo ago)01.9k↓30.7%[4 PRs](https://github.com/yardinternet/wp-data/pulls)2MITPHPPHP &gt;=8.2CI passing

Since Jun 11Pushed 3mo ago2 watchersCompare

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

READMEChangelog (10)Dependencies (16)Versions (23)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;
```

### 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

49

—

FairBetter than 95% of packages

Maintenance84

Actively maintained with recent releases

Popularity22

Limited adoption so far

Community18

Small or concentrated contributor base

Maturity62

Established project with proven stability

 Bus Factor1

Top contributor holds 53.1% of commits — single point of failure

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 ~46 days

Total

15

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] (37 commits)")[![ictbeheer](https://avatars.githubusercontent.com/u/14947039?v=4)](https://github.com/ictbeheer "ictbeheer (15 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)")[![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

[binaryk/laravel-restify

Laravel REST API helpers

651399.1k](/packages/binaryk-laravel-restify)[dragon-code/laravel-deploy-operations

Performing any actions during the deployment process

240173.5k2](/packages/dragon-code-laravel-deploy-operations)[moe-mizrak/laravel-openrouter

Laravel package for OpenRouter (A unified interface for LLMs)

153107.2k2](/packages/moe-mizrak-laravel-openrouter)[helgesverre/extractor

AI-Powered Data Extraction for your Laravel application.

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

Laravel Auto CRUD helps you streamline development and save time.

28711.8k2](/packages/mrmarchone-laravel-auto-crud)[relaticle/custom-fields

User Defined Custom Fields for Laravel Filament

15828.6k](/packages/relaticle-custom-fields)

PHPackages © 2026

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