• I have just exported a bunch of posts to a new site and need to delete them from the old one. In the past I’ve used WP CLI to do this but this new case has a new challenge. I need to delete a range of posts less than or equal to a specific ID number for which the category ID equals a specific number. So, basically delete each post with ID<=##### AND category = ##.

    I have used wp db query in the past but don’t know to do it this time because the posts table has no column for the category ID. A better database design would only store actual posts in the posts table and have a column for category ID because every post should have at least one category, but instead someone at WordPress decided to use the posts table to store all sorts of things most of which are not posts.

    If I didn’t need to filter based on category ID, I would use the following for the deletion:

    wp post delete $(wp db query 'SELECT ID FROM wp_posts WHERE ID<"89830" AND post_type="post" AND post_status="publish";' --skip-column-names)

    If I just needed to delete the category I would use this:

    wp post delete $(wp post list --cat=25 --format=ids) --skip-plugins --skip-theme

    How do I alter either of those to include the other? Preferable the second one should be alterable so that someone can type something like –id<=89830 unfortunately that is not an option. This means the first method will likely need to include a join with whatever table contains the record matching the post to a category which I believe to be the taxonomy table but I’m not sure exactly.

    Could someone at WordPress please make up for this obvious lack of functionality which should’ve been in place years ago by posting SQL in response to this question which can be plugged into wp db query and achieve the desired result?

Viewing 1 replies (of 1 total)
  • To filter posts by post ID range and category using WP-CLI, you can use the wp query command with the --category_name and --post__in parameters. Here’s an example command:

    wp query --post__in=$(wp eval 'echo implode(",", range(10,20));') --category_name=category-slug
    

    In this example, replace 10 and 20 with the first and last post IDs in the range you want to filter by, and replace category-slug with the slug of the category you want to filter by.

    The 'wp eval' command will generate a comma-separated list of post IDs in the specified range, which can then be used as the value of the --post__in parameter. Additionally, the --category_name parameter can be used to filter the results by the specified category.

    You can also add other parameters to further customize the query, such as --post_type to filter by a specific post type or --posts_per_page to limit the number of results returned.

    Note: Ensure that you execute this command from the root directory of your WordPress installation. Additionally, bear in mind that filtering posts by ID range can be a resource-intensive process if you have a large number of posts.

Viewing 1 replies (of 1 total)
  • The topic ‘How Do I Filter by Post ID Range and Category Using WP CLI?’ is closed to new replies.