admin管理员组

文章数量:1123940

I am in the process of learning WP-CLI and I typed in the command line mistakenly wp post generate instead of wp post generate --help

Now I have generated the default 100 posts, however, this is a good opportunity for me to try to delete these 100 posts using WP-CLI.

However, I could not figure it out. So is it possible to delete a range of posts by ID like for example from 1970 to 2070 or using the time they were generated as they are all generated at the same time all this with WP-CLI.

I am in the process of learning WP-CLI and I typed in the command line mistakenly wp post generate instead of wp post generate --help

Now I have generated the default 100 posts, however, this is a good opportunity for me to try to delete these 100 posts using WP-CLI.

However, I could not figure it out. So is it possible to delete a range of posts by ID like for example from 1970 to 2070 or using the time they were generated as they are all generated at the same time all this with WP-CLI.

Share Improve this question asked Jul 3, 2020 at 17:14 FakhriAzFakhriAz 331 silver badge5 bronze badges 2
  • Related: github.com/wp-cli/wp-cli/issues/3607 – Jesse Nickles Commented Mar 21, 2024 at 9:49
  • Related: wordpress.stackexchange.com/questions/137304/… – Jesse Nickles Commented Mar 21, 2024 at 9:54
Add a comment  | 

3 Answers 3

Reset to default 2

Nope, unfortunately not... I was looking for the same thing more or less (range by ID instead dates) and since wp media regenerate supports entering an ID range I thought it should be possible...

# Re-generate all thumbnails that have IDs between 1000 and 2000.
$ seq 1000 2000 | xargs wp media regenerate
Found 4 images to regenerate.
1/4 Regenerated thumbnails for "Vertical Featured Image" (ID 1027).
2/4 Regenerated thumbnails for "Horizontal Featured Image" (ID 1022).
3/4 Regenerated thumbnails for "Unicorn Wallpaper" (ID 1045).
4/4 Regenerated thumbnails for "I Am Worth Loving Wallpaper" (ID 1023).
Success: Regenerated 4 of 4 images.

You could export your posts by date range first via

wp export --dir=/tmp/ --user=admin --post_type=post --start_date=1970-01-01 --end_date=2070-12-31

then open the generated XML-file(s), select all <wp:post_id>XYZ</wp:post_id> in your editor (like SublimeText or VSCode), get rid of the markup and format the IDs as space-separated list and then use the gathered IDs to bulk delete like this

wp post delete 1001 1009 1028 1030 1044 1050

Very cumbersome, I know... Maybe create an issue over at the according Github-repo and request this feature?

For example, if you want to delete posts with IDs from 1970 to 2070, you would run below command

wp post delete $(seq 1970 2070) --force

It is possible with some complex WP-CLI commands.

Specifically, you can list a range of WordPress posts in a subshell using the wp post list command, and then delete that range of posts with wp post delete as below:

WARNING: BACKUP YOUR DATABASE BEFORE TRYING THIS!!!

wp post delete $(wp post list --post_type='post' --format=ids --posts_per_page=1)

In the above example, the most recent post will be deleted (not the oldest post). I strongly suggest you first test the subshell ouput beforehand like this:

wp post list --post_type='post' --format=ids --posts_per_page=1

This will tell you the ID number of the post that will be deleted. Check this ID number against the ID number of your most recent post (for example) if that's the post(s) you are wanting to delete. If all looks good, you can increase the number of posts you want to delete, e.g. --posts_per_page=10

Some posts types will cause a warning like:

Warning: Posts of type 'topic' do not support being sent to trash.

In that case you must append --force to your command, which will permanently delete them:

WARNING: PERMANENT DATA LOSS USING THE COMMAND BELOW!!!

wp post delete $(wp post list --post_type='topic' --format=ids --posts_per_page=10) --force

本文标签: wp cliIs it possible to delete a range of posts by ID with WPCLI