Try this:
$imgs = get_posts("post_type=attachment&numberposts=-1");
foreach($imgs as $img){
$file = get_attached_file($img->ID);
if(!file_exists($file)){
wp_delete_post( $img->ID, false );
}
}
Related Posts:
- What’s the database relationship between a post and its attachments
- How might I retrieve a featured post image from an external WP site and display it as a link back?
- How to delete outdated, wrongly sized images in _wp_attachment_metadata?
- Size article thumbnails and retrieve them directly from database
- How to convert srcset links from https to http?
- Media library images “missing” after migrating them using SQL
- What is the difference between an ORM and an ODM?
- Rename a table in MySQL
- How should I tackle –secure-file-priv in MySQL?
- Can’t connect to MySQL server on ‘127.0.0.1’ (10061) (2003)
- How To Retrieve An Image Attachment’s Alt Text?
- Keeping WP database synced across multiple developers using git
- How to define composite keys with dbDelta()
- Is it possible to define two databases for one installation?
- Connect forms in WP to external database
- Hook into $wpdb
- How to remove images from showing in a post with the_content()?
- How to display a shortcode caption somewhere other than the_content
- Connecting to external oracle database
- Bulk Update of Custom Post Type Post Status through SQL
- How do I add indexes to WordPress Database?
- Multiple installation in a single database
- Allowing post attachments without allowing to insert in text
- How do I create Comma Separated list of attached image ids?
- Huge wp_options table
- Can’t save ajax value to database
- Cannot unserialize WordPress serialized values in `wp_options` table?
- Block search SQL from happening
- wp_options table has duplicate rows and primary key / autoincrement has been removed
- What is the purpose of storing `siteurl` in database?
- Installation with sql server express edition
- query attachment images … if no attachments -> get attachments of parent page?
- Get all tags not just first 10 with wp api 2.0
- Should non-WordPress data get its own DB?
- Get the url of the full sized attachment image using post ID?
- Custom page template to display all image attachments
- Is there a way to see history of WordPress database changes?
- Where does the actual data stored by add_post_meta
- How can I attach hotlinked images in posts/pages within the same server?
- WordPress Image Attachment using remote image
- Output custom content not from the posts table?
- Exporting table to csv works in the admin but exports HTML data when used on the front end
- wp search-replace (not executing)
- How to have an external DB read and get data from my wordpress database
- Import users from old WP site to new one
- SSO system between 2 WordPress installs on different servers
- How do you get the full size url of an attachment image
- is $wpdb->get_results() safe enough
- Does post_meta data need to be unserialzed?
- Can’t update WordFence Options, clear data manually
- Is it possible to have more than 1 MySQL database in my WordPress blog?
- How to fetch records from database WordPress
- restore a db after bad backup
- Best practice to import user base (subscribers) from one website to another?
- AJAX wp-mysql running too slow
- Some weird users in database
- Delete/Replace words from X to Y in post content
- Location of image metadata on my server
- How to create more than one new wpdb object?
- Multiple WordPress sites installs sharing the same database but with different content and domains
- Remote database -> massive response time increase?
- Custom Post Type not showing anything added by code anymore?
- Is it good practice to import/export a blog to test a redesign?
- Getting value from database table depending on field value
- Getting “Error Establishing a Database Connection” on localhost in new Mac OSX Lion environment
- Secure way to use name_save_pre?
- wpdb->insert not inserting first variable
- Database tables for sports betting integrated in WordPress
- change the year on all of my custom post types?
- New installation fatal error in 5.9.3 at edit or create entry
- Storing transients giving database error
- I want to replicate a wordpress website over multiple servers
- Is there anyway to split posts to different databases?
- wp-cli + valet: “Error establishing a database connection”
- How do I add a medium to the WordPress database with my own plugin?
- How to echo images Urls from a wordpress post, that are relally in the post
- local wordpress broke after changing URL
- Unable to login to a https website via the app after changing from http
- mysqli_query(): MySQL server has gone away for Curl Request
- Allow users to upload attachments to certain posts
- Is it safe to add INDEX to a column in WordPress database?
- Restore Old Database Over Newer WP & Plugin Files
- WP website showing blank page after moving from subdomain to main domain
- create a table using user meta custom data
- Database structure for thousands of posts
- WPCLI search and replace in a particlar site dir effect another site-dir
- How do I find out from my database what version of WordPress I need?
- How to create a database table?
- Extracted CSV as Array for Custom Query Loop
- Hang Up Followed By Can’t select database
- Database error after importing WordPress
- One WP Database outside localhost and two connections
- Create a custom attachment thumbnail for quick page load
- Pulling values from a sepcific row in table
- Every time I update or install a plugin I get “Error Establishing a Database Connection in WordPress”
- import (migration) user database to wp-users
- Create hundreds of users with just ID in phpMyAdmin
- What is best for saving lot of extra detail of user?
- Insert Extra fields added in the front end registration form to DB
- WP Multisite Users Not Linking