`wp_delete_post` returns NULL

I am trying to delete a lot of WordPress posts programmatically using wp_delete_post, but not all posts are deleted successfully. In some cases I get NULL as a return value for wp_delete_post($row-post_id, true) and the post isn't deleted from the database.

There are both post and page post_types amongst those not deleted posts, as well as custom post types.

What can be the issue there, why aren't some posts deleted, while many others are deleted successfully?

Topic wp-delete-post Wordpress

Category Web


I realized that I was trying to delete a post while my current site wasn't the same one. My WordPress is multisite.

switch_to_blog($siteId); solved the issue.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.