How to Copy Post Content HTML from WordPress with WP-CLI

Date: 11/8/19  2 Minutes To Read

WP-CLI is a command line tool that comes with your WordPress Hosting package. You can use it to manage your WordPress site with quick and intuitive command line instructions. Utilized properly, WP-CLI can drastically speed up your workflow and replace tedious dashboard operations with efficient scripts you can repeat and automate.

Using the new Gutenberg editor lets you place content into convenient blocks. But what happens if you need to copy the content of a long int HTML? You would need to preview each Gutenberg block separately as HTML, copy, and paste it into a local HTML file. For a long post, this is incredibly time-consuming and will introduce multiple opportunities for error. Using WP-CLI can ensure you copy the HTML content perfectly and preserve it in an HTML file.

Copy Post Content from WordPress With WP-CLI One-liner

WP-CLI provides an easy shortcut for accessing specific database tables where your content. Using the command below you can have WP-CLI go into the database, get post content, and output that content to a file:

Send content to a file in the same directory:

$ wp post get 123 --field=content > file.html

Second content to a file in your home directory by adding a tilde ~:

$ wp post get 123 --field=content > ~/file.html

How to Copy Post Content to a File with WP-CLI

Here are the steps on how to use the above commands and where they need to go:

  1. Log into your terminal
  2. Navigate to the WordPress site directory (where the core files reside)
  3. Get your post ID using WP-CLI or previewing a URL from the post list in the dashboard (URL will contain ?post=###)
  4. Use one of the above code snippets with the post ID number

When you visit your file, you will notice that it is filled with HTML content you can save or paste into WordPress as needed.

Learn more about WP-CLI, the command-line interface for WordPress, and how to streamline your WordPress maintenance workflows!

CM
Christopher Maiorana Content Writer II

Christopher Maiorana joined the InMotion community team in 2015 and regularly dispenses tips and tricks in the Support Center, Community Q&A, and the InMotion Hosting Blog.

More Articles by Christopher

Was this article helpful? Join the conversation!