The docs show how to delete by ID. How do I delete by slug/title?


There is no wp-cli command to delete a post by title, but you could easily roll your own.

Adding something like this in you current themes function.php file would work:

if (defined('WP_CLI') && WP_CLI) {
       * Delete post by title.
       * ## OPTIONS
       * <title>
       * : The title of the post you would like to delete.
       * [--type=<type>]
       * : The post type.
       * [--force-delete]
       * : Deletes the post without moving it to trash.
       * ## EXAMPLES
       *     wp post deleteByTitle "Hello world"
       * @when after_wp_load
      function deleteByTitle($args, $assoc_args)
          global $wpdb;
          list($title) = $args;
          $type = $assoc_args['type'] ?? 'post';
          $force_delete = $assoc_args['force-delete'] ?? false;
          $result = $wpdb->get_results(
                  $wpdb->prepare("SELECT ID FROM $wpdb->posts
                                  WHERE post_type = %s &&
                                        post_status = 'publish' &&
                                        post_title = %s",
                  $type, $title)
          if ($result) {
              wp_delete_post($result[0]->ID, $force_delete);

    WP_CLI::add_command('post', 'CYCLONECODE_WP_CLI_COMMANDS');

You should then be able to use the command like this:

wp post deleteByTitle "Hello world"
wp post deleteByTitle "foo" --type="custom_post_type"
wp post deleteByTitle "bar" --force-delete

You could also first make a query using wp-cli and then use the returned ID to then execute a post delete command:

wp post delete $(wp db query \
   FROM wp_posts \
   WHERE post_title = "POST_TITLE" && post_status="publish" && post_type="post"' \
   --skip-column-names \

In the above you will need to replace POST_TITLE with the title of the post you would like to delete.

  • 1
    By "easily" you mean that you just saved me half a day's worth of work. Thanks!
    – John Dee
    Dec 15 '21 at 7:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.