WordPress Basics

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.

Add categories, a child category and a tag to a post

Description: Create a category Travel with a child category India, and a tag tips. Then create a published post Visit Jaipur in the category India with the tag tips, and list the categories with the post count.

Hint:

  • wp term create category "Name" --parent=ID makes a child category.
  • wp post create has the flags --post_category and --tags_input.

Solution

Terminal output

$ wp term create category "Travel" –porcelain
2
$ wp term create category "India" –parent=2 –porcelain
3
$ wp term create post_tag "tips" –porcelain
4
$ wp post create –post_title="Visit Jaipur" –post_status=publish –post_category=3 –tags_input=tips –porcelain
5
$ wp term list category –fields=term_id,name,parent,count
term_id name parent count
3 India 2 1
2 Travel 0 0
1 Uncategorized 0 1
$ wp term list post_tag –fields=term_id,name,count
term_id name count
4 tips 1

How it works

  • The first category you make gets the ID 2, because ID 1 is Uncategorized. We used the ID of Travel as the parent of India.
  • The post is in India only, so the count of India is 1 and the count of Travel stays 0, as WordPress does not count child posts in the parent by default here.

Read Categories and Tags.

Change the site title and tagline and check them on the site

Description: Change the site title to Tutorials Class Demo and the tagline to Learn WordPress step by step. Then check that the new title is in the <title> tag of the home page.

Hint:

  • The options are blogname and blogdescription.
  • In the dashboard it is Settings > General.

Solution

Terminal output

$ wp option update blogname "Tutorials Class Demo"
Success: Updated 'blogname' option.
$ wp option update blogdescription "Learn WordPress step by step"
Success: Updated 'blogdescription' option.
$ wp option get blogname
Tutorials Class Demo
$ curl -s http://example.com/ | grep -o "<title>[^<]*</title>"
<title>Tutorials Class Demo &#8211; Learn WordPress step by step</title>

How it works

  • Settings are rows in the wp_options table. wp option update changes one row.
  • The theme prints the site title and the tagline in the title tag, so the home page shows the new text at once.

Read WordPress Settings.

Set a static front page and a posts page

Description: Create two pages, Home and News. Make Home the front page of the site and News the page which lists the blog posts. Check the setting.

Hint:

  • The options are show_on_front, page_on_front and page_for_posts.

Solution

Terminal output

$ wp post create –post_type=page –post_title="Home" –post_status=publish –porcelain
5
$ wp post create –post_type=page –post_title="News" –post_status=publish –porcelain
6
$ wp option update show_on_front page
Success: Updated 'show_on_front' option.
$ wp option update page_on_front 5
Success: Updated 'page_on_front' option.
$ wp option update page_for_posts 6
Success: Updated 'page_for_posts' option.
$ curl -s -o /dev/null -w "%{http_code}\n" http://example.com/news/
200
$ wp option get page_on_front
5

How it works

  • show_on_front is page when you use a static page. The two other options keep the page IDs.
  • The News page now shows the latest posts at /news/.

Read WordPress Pages.

Create two users and compare their capabilities

Description: Create a user meera with the role editor and a user karan with the role subscriber. Print for both users if they can edit_others_posts and publish_posts.

Hint:

  • Use wp user create with --role.
  • The PHP function user_can( $user_id, $capability ) checks a capability.

Solution

Terminal output

$ wp user create meera meera@example.com –role=editor –user_pass=Pass#1234 –porcelain
2
$ wp user create karan karan@example.com –role=subscriber –user_pass=Pass#1234 –porcelain
3
$ wp user list –fields=ID,user_login,roles
ID user_login roles
1 admin administrator
3 karan subscriber
2 meera editor
$ wp eval 'foreach ( array( 2 => "meera", 3 => "karan" ) as $id => $name ) { foreach ( array( "edit_others_posts", "publish_posts" ) as $cap ) { echo $name, " ", $cap, ": ", user_can( $id, $cap ) ? "yes" : "no", "\n"; } }'
meera edit_others_posts: yes
meera publish_posts: yes
karan edit_others_posts: no
karan publish_posts: no

How it works

  • The role gives a set of capabilities. An editor can publish and edit the posts of other users, and a subscriber can do neither.
  • user_can() checks any user by ID, while current_user_can() checks the user who is logged in.

Read Users and Roles.

Change the permalink structure and print a post URL

Description: Create a post Permalink practice dated 10 February 2026. Print its URL with the Post name structure. Then change the structure to /%year%/%monthnum%/%postname%/ and print the URL again.

Hint:

  • wp rewrite structure '/%postname%/' sets the structure.
  • get_permalink( $id ) gives the URL of a post.

Solution

Terminal output

$ wp post create –post_title="Permalink practice" –post_status=publish –post_date="2026-02-10 09:00:00" –porcelain
5
$ wp rewrite structure '/%postname%/'
Success: Rewrite structure set.
Success: Rewrite rules flushed.
$ wp eval 'echo get_permalink( 5 );'
http://example.com/permalink-practice/
$ wp rewrite structure '/%year%/%monthnum%/%postname%/'
Success: Rewrite structure set.
Success: Rewrite rules flushed.
$ wp eval 'echo get_permalink( 5 );'
http://example.com/2026/02/permalink-practice/
$ curl -s -o /dev/null -w "%{http_code}\n" http://example.com/2026/02/permalink-practice/
200

How it works

  • The structure is saved in the option permalink_structure. The command also flushes the rewrite rules.
  • The new URL works at once (code 200). If a URL gives 404 after a change, flush the rules.

Read WordPress Permalinks.

Create a navigation menu with three links

Description: Create a menu named Top Menu with three items: Home (the home page address), About (a page of your site) and Docs (the link https://wordpress.org/documentation/). List the items.

Hint:

  • wp menu create, wp menu item add-post and wp menu item add-custom.

Solution

Terminal output

$ wp post create –post_type=page –post_title="About" –post_status=publish –porcelain
5
$ wp menu create "Top Menu"
Success: Created menu 2.
$ wp menu item add-custom top-menu "Home" http://example.com/
Success: Menu item added.
$ wp menu item add-post top-menu 5 –title="About"
Success: Menu item added.
$ wp menu item add-custom top-menu "Docs" https://wordpress.org/documentation/
Success: Menu item added.
$ wp menu item list top-menu –fields=db_id,title,type,link
db_id title type link
6 Home custom http://example.com/
7 About post_type http://example.com/about/
8 Docs custom https://wordpress.org/documentation/

How it works

  • A menu is a term of the taxonomy nav_menu. Each link is a post of the type nav_menu_item.
  • A custom item keeps any URL, while add-post links to a page or post by ID and keeps the link updated.

Read WordPress Menus.

Back up the database, delete a post and restore it

Description: Create a post Important post. Make a database backup, delete the post for ever, and then restore the backup. Show the number of posts before the delete, after the delete and after the restore.

Hint:

  • wp db export file.sql and wp db import file.sql.
  • wp post list --format=count gives the number of posts.

Solution

Terminal output

$ wp post create –post_title="Important post" –post_status=publish –porcelain
5
$ wp post list –post_type=post –format=count
2
$ wp db export backup.sql
Success: Exported to 'backup.sql'.
$ wp post delete 5 –force
Success: Deleted post 5.
$ wp post list –post_type=post –format=count
1
$ wp db import backup.sql
Success: Imported from 'backup.sql'.
$ wp post list –post_type=post –format=count
2

How it works

  • The export writes all tables as SQL commands in one file.
  • The import runs the file again and brings the database back to the moment of the backup.
  • Keep the backup file outside the web folder on a real site, and copy wp-content too.

Read Backup and Update.