没有wp cli命令可以按标题删除帖子,但您可以轻松地滚动自己的帖子。
在当前主题函数中添加类似的内容。php文件可以工作:
if (defined(\'WP_CLI\') && WP_CLI) {
class CYCLONECODE_WP_CLI_COMMANDS extends WP_CLI_Command
{
/**
* 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\');
}
然后,您应该能够使用如下命令:
wp post deleteByTitle "Hello world"
wp post deleteByTitle "foo" --type="custom_post_type"
wp post deleteByTitle "bar" --force-delete
您还可以首先使用wp cli进行查询,然后使用返回的ID执行
post delete
命令:
wp post delete $(wp db query \\
\'SELECT ID \\
FROM wp_posts \\
WHERE post_title = "POST_TITLE" && post_status="publish" && post_type="post"\' \\
--skip-column-names \\
)
在上述情况下,您需要更换
POST_TITLE
带有您要删除的帖子标题。