wp-cli/entity-command
WP-CLI commands to manage WordPress entities: comments, menus, options, posts, sites, terms, and users. Create, update, delete, and moderate content from the command line, with support for listing and bulk operations.
Installation:
composer require wp-cli/entity-command
Ensure WP-CLI is installed globally (wp --version) and WordPress is accessible via wp commands.
First Use Case:
List all comments for a post (e.g., ID 123) in JSON format:
wp comment list --post_id=123 --format=json
Key Files:
README.md: Command reference and examples.features/: Behat tests for edge cases.tests/: PHPUnit unit tests for core logic.CRUD Operations:
wp comment create with associative args (e.g., --comment_post_ID=123 --comment_content="Test").wp comment get + wp comment update:
wp comment get 456 --field=comment_content | xargs -I {} wp comment update 456 --comment_content={}
wp comment delete $(wp comment list --status=spam --format=ids) --force
Querying Data:
wp comment list --meta_key=custom_field --meta_value="value" --fields=comment_ID,comment_author
wp comment list --format=csv > comments.csv
Meta Management:
wp comment meta add 789 moderation_status "pending" --format=json
wp comment meta patch update 789 review_data "feedback" "Thanks for the input"
Automation:
wp comment generate --count=50 --post_id=123 --format=ids | xargs -I % wp comment meta add % user_role "subscriber"
wp comment list --status=trash --format=ids | xargs -I % wp comment delete %
Notes (WordPress 6.9+):
wp comment create --comment_post_ID=123 --comment_type=note --comment_content="Needs review" --comment_parent=456
wp comment meta add 999 _wp_note_status resolved
exec() or Process facade to call WP-CLI commands from Laravel:
$output = Artisan::call('wp comment count 123', ['format' => 'json']);
$comments = json_decode($output, true);
wp myplugin sync-comments).Permissions:
wp comment delete --force require admin privileges. Use --user=admin if needed:
wp comment delete 123 --force --user=admin
unfiltered_html capability is missing.Data Serialization:
--format=json for complex data:
wp comment meta add 456 review_data '{"status": "approved", "notes": ["Great work!"]}' --format=json
wp comment meta pluck 456 review_data "notes.0" # Fails if path is incorrect
WordPress Version Quirks:
comment_type=note) are only supported in WordPress 6.9+. Check version first:
wp core version | grep -q "6.9" || echo "Notes not supported"
--network flag for network-wide operations:
wp comment list --network --status=spam
Performance:
wp comment list without limits on large sites. Use --number=100:
wp comment list --number=100 --fields=comment_ID,comment_author
xargs to reduce overhead.Edge Cases:
--status=trash is specified.--comment_parent=ID.--porcelain to extract raw IDs for scripting:
wp comment list --status=spam --format=ids --porcelain
WP_CLI_DEBUG=1 wp comment create --comment_post_ID=123 --comment_content="Test"
wp comment exists 456 || echo "Comment not found"
// app/Services/WPCommentMeta.php
public function addCustomMeta(int $commentId, string $key, $value): void
{
exec("wp comment meta add {$commentId} {$key} '{$value}' --format=json");
}
wp-cli/entity-command's after_comment_create hook if available).$this->artisan('wp comment generate --count=1 --post_id=123')
->expectsQuestion('Confirm?', 'yes')
->assertExitCode(0);
wp-cli.yml:
formats:
table: "yaml" # Force YAML output for all tables
alias wpcc='wp comment count'
alias wpcl='wp comment list'
```markdown
### Laravel-Specific Tips
1. **Service Provider**:
Bind WP-CLI commands to Laravel’s container for dependency injection:
```php
// app/Providers/WPCLIServiceProvider.php
public function register()
{
$this->app->singleton('wp-cli', function () {
return new WPCLICommand();
});
}
// app/Console/Kernel.php
protected function schedule(Schedule $schedule)
{
$schedule->command('wp comment delete --status=spam --force')
->dailyAt('03:00');
}
// app/Models/Comment.php
public static function syncWithWP()
{
$comments = json_decode(exec('wp comment list --format=json'), true);
foreach ($comments as $data) {
self::updateOrCreate(['id' => $data['comment_ID']], $data);
}
}
How can I help you explore Laravel packages today?