Fake post data with wp-cli. Made for WordPress.
Relies on nelmio/alice and fzaninotto/Faker.
wp package install trendwerk/faker
Requires wp-cli >= 0.23.
wp faker fake <files>...
Parameter | Default | Required | Description |
---|---|---|---|
<file> |
null |
Yes | Location to an Alice YAML file |
wp faker delete
Deletes all fake data.
The YAML file supports:
Fields | Description |
---|---|
WP_Post |
All properties from wp_insert_post |
meta |
Post meta |
terms |
Terms for taxonomies, see Terms |
acf |
Advanced Custom Fields fields, see ACF |
Your YAML file(s) could look like any the examples below.
For more understanding of the internals:
- The YAML file is interpreted by nelmio/alice;
- Any of the functions from fzaninotto/Faker are available;
- Additionaly, the
<terms>
function is provided by this library.
Trendwerk\Faker\Entity\Post:
post{1..100}:
post_content: <paragraphs(4, true)>
post_title: '<sentence()>'
Generates 100 posts with a title and content.
Trendwerk\Faker\Entity\Post:
post{1..100}:
post_title: '<sentence()>'
post_type: 'testimonials'
Generates 100 posts from the post type testimonials
with a title.
Trendwerk\Faker\Entity\Post:
post{1..100}:
post_title: '<sentence()>'
post_type: 'testimonials'
meta:
name: '<name()>'
address: '<address()>'
Generates 100 testimonials with a title and a custom field called name
and one called address
.
Trendwerk\Faker\Entity\Post:
post{1..100}:
post_content: <paragraphs(3, true)>
post_title: '<sentence()>'
terms:
category: <terms('category', 1)>
post_tag: <terms('post_tag', 7)>
Generates 100 posts with a title, content, 1 random category and 7 random tags.
Using <terms>
is not required. You could also provide an array of integers yourself or use randomElements.
Parameter | Default | Required |
---|---|---|
taxonomy |
null |
Yes |
amount |
1 |
No |
Trendwerk\Faker\Entity\Post:
post{1..100}:
post_content: <paragraphs(3, true)>
post_title: '<sentence()>'
acf:
name: '<name()>'
address: '<address()>'
Generates 100 posts with a title, content, and two filled ACF fields: name
and address
.
In ACF, it is possible to have multiple fields with the same name. This could cause formatting conflicts when faking data with this library. If you have two fields with the same name, using the unique field key is recommended:
Trendwerk\Faker\Entity\Post:
post{1..100}:
post_content: <paragraphs(3, true)>
post_title: '<sentence()>'
acf:
field_56cf2f782e9b1: '<name()>' # Name
address: '<address()>'
Currently the only type of supported attachments are images.
Trendwerk\Faker\Entity\Image:
image{1..3}:
data: '<image()>'
Generates 3 image attachments. Images are provided by Faker, which in turn are provided by LoremPixel.
# image.yml
Trendwerk\Faker\Entity\Image:
image{1..3}:
data: '<image()>'
# post.yml
Trendwerk\Faker\Post:
post{1..1}:
post_content: <paragraphs(3, true)>
post_title: '<sentence()>'
meta:
_thumbnail_id: '@image*->id'
You can now supply both files to wp faker fake
:
wp faker fake image.yml post.yml
Make sure you load the file that contains the referenced objects first.