Day 3: Managing Content with WP-CLI

Creating and Managing Posts, Pages, and Custom Post Types

WP-CLI provides powerful commands for managing your WordPress content. Let’s explore how to create and manage posts, pages, and custom post types.

  • To create a new post:
wp post create --post_type=post --post_title="My New Post" --post_content="This is the content of my new post." --post_status=publish
  • To create a new page:
wp post create --post_type=page --post_title="About Us" --post_content="This is the content of our About Us page." --post_status=publish
  • To create a custom post type (assuming you have already registered the custom post type):
wp post create --post_type=book --post_title="The Great Gatsby" --post_content="A novel by F. Scott Fitzgerald." --post_status=publish

You can also update existing content using the wp post update command. For example, to update the title of a post with ID 123:

wp post update 123 --post_title="Updated Post Title"

Importing and Exporting Content

WP-CLI makes it easy to import and export content using the WordPress eXtended RSS (WXR) format.

  • To export all content to a WXR file:
wp export --dir=/path/to/export --skip_comments
  • To import content from a WXR file:
wp import /path/to/import/file.wxr

Managing Post Metadata

You can use WP-CLI to manage post metadata, such as featured images and custom fields.

  • To set a featured image for a post:
wp post meta set 123 _thumbnail_id 456
  • To add a custom field to a post:
wp post meta add 123 custom_field_key "Custom field value"

Best Practices

When managing content with WP-CLI, consider the following best practices:

  • Use descriptive post titles and slugs for better SEO
  • Organize your content using categories and tags
  • Regularly backup your content before making bulk changes
  • Test your commands on a staging site before applying them to your live site

In the next lesson, we’ll explore how to manage users and their roles using WP-CLI. Stay tuned!