Download the PHP package yard/data without Composer
On this page you can find all versions of the php package yard/data. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package data
WP Data Objects
Powerful data objects for WordPress.
Requirements
Installation
You can install this package with Composer:
You can publish the config file with:
Usage
PostData
Creating PostData
PostData can be created from the global \WP_Post object:
From an array:
Or from an Eloquent Model using Corcel:
Custom PostData
Creating a VacancyData object by extending PostData:
Enables you to create VacancyData object in the same way as PostData:
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:
`
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.
Now every time you call Yard\Data\PostData::from($post) on a custom post type the mapped instance will be returned.
Meta Fields
Using the Meta Attribute
Adding a meta field with a meta_key of vacancy_email to your VacancyData looks like this:
This approach is functionally equivalent to using:
You can also specify any available Data Object, and the meta value will be cast to that Data Object:
The MetaPrefix Class Attribute
If all of your meta fields are prefixed with the same prefix you can use the MetaPrefix attribute:
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:
Taxonomy Terms
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:
This approach is functionally equivalent to using:
or:
The TaxonomyPrefix Class Attribute
If all of your taxonomies are prefixed with the same prefix you can use the TaxonomyPrefix attribute:
Reading Terms from your Data Object
Because Terms are a Collection you can use any of the available collection methods to read the terms from your data object. Here are some common examples:
Extending TermData
You can add extra meta fields to taxonomies by extending the default TermData object
In your PostData object you have to specify the data class used for a specific taxonomy:
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
Extending CommentData
You can add extra meta fields to comments by extending the default CommentData object
UserData
Create UserData from current user: