WP-CLI
The plugin registers the content-relations command group. All examples assume the relation type is related_to unless noted; change --type= as needed.
Quick reference
| Command | Purpose |
|---|---|
wp content-relations list --post=ID [--type=TYPE] | List relations for a post |
wp content-relations add FROM_ID TO_ID --type=TYPE | Add one relation |
wp content-relations remove FROM_ID TO_ID --type=TYPE | Remove one relation |
wp content-relations count --post=ID [--type=TYPE] | Count relations |
wp content-relations check [--fix] [--verbose] [--batch-size=N] | Integrity check (optionally fix invalid/orphaned rows) |
wp content-relations sync [--dry-run] [--batch-size=N] | Same as check; use --dry-run to preview, no fix |
wp content-relations schema [--format=json] | Export relation type schema |
wpcr | Short alias for content-relations (e.g. wpcr list --post=123) |
Run wp content-relations --help and wp content-relations <command> --help for full options.
List relations
bash
# All relations for post 123 (table)
wp content-relations list --post=123
# Only type "related_to", output as JSON
wp content-relations list --post=123 --type=related_to --format=json
# CSV for scripting
wp content-relations list --post=123 --format=csvAdd and remove
bash
# Add: post 123 → post 456, type "related_to"
wp content-relations add 123 456 --type=related_to
# Remove that relation
wp content-relations remove 123 456 --type=related_toCount and verify
bash
# Total relations for post 123
wp content-relations count --post=123
# Count only "related_to"
wp content-relations count --post=123 --type=related_to
# Check DB integrity (orphaned rows, missing posts); fix with --fix
wp content-relations check --verbose
wp content-relations check --fix
# Sync: same as check; --dry-run previews without fixing
wp content-relations sync --dry-run
wp content-relations sync --batch-size=500
# Short alias
wpcr list --post=123
wpcr add 123 456 related_toSchema export
bash
# All registered relation types as JSON
wp content-relations schema --format=jsonBatch import from a file
Create a file pairs.txt with one pair per line: from_id to_id (e.g. 123 456). Then:
bash
while read -r from to; do
wp content-relations add "$from" "$to" --type=related_to
done < pairs.txtOr with xargs (one pair per line, space-separated):
bash
cat pairs.txt | xargs -L1 sh -c 'wp content-relations add $1 $2 --type=related_to' _Use after migration to bulk-import relations from exported data.