Create a post and a page with WP-CLI

Description: Create a published post titled My first post with some text, and a published page titled Contact. Then list the posts and the pages with their ID and status.

Hint:

  • Use wp post create with --post_type=page for the page.
  • The flag --porcelain prints only the new ID.
  • wp post list --post_type=page lists the pages.

Solution

Terminal output

$ wp post create –post_title="My first post" –post_content="Hello WordPress!" –post_status=publish –porcelain
5
$ wp post create –post_type=page –post_title="Contact" –post_status=publish –porcelain
6
$ wp post list –post_type=post –fields=ID,post_title,post_status
ID post_title post_status
5 My first post publish
1 Hello world! publish
$ wp post list –post_type=page –fields=ID,post_title,post_status
ID post_title post_status
6 Contact publish
2 Sample Page publish
3 Privacy Policy draft

How it works

  • wp post create inserts a row in the wp_posts table. The post_type decides if it is a post or a page.
  • The default type is post, so only the page needs --post_type=page.
  • The list shows also the sample content of a fresh site.

Read WordPress Posts and WordPress Pages.


Learn more about the similar topics:
Tutorials
No Content Found.
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.