﻿{"id":54299,"date":"2025-05-24T09:03:25","date_gmt":"2025-05-24T09:03:25","guid":{"rendered":"https:\/\/bookwormhead.com\/?p=54299"},"modified":"2025-05-24T09:03:25","modified_gmt":"2025-05-24T09:03:25","slug":"fix-broken-media-library-in-wordpress","status":"publish","type":"post","link":"https:\/\/bookwormhead.com\/cms\/fix-broken-media-library-in-wordpress\/","title":{"rendered":"Fix Broken Media Library in WordPress"},"content":{"rendered":"<p>If you&#8217;re running a local WordPress site or recently migrated data and started seeing strange behavior in the Media Library\u2014like missing images, broken grid views, or a flood of PHP warnings\u2014you\u2019re not alone.<\/p>\n<p>Many developers encounter issues where attachments exist in the database but are missing essential properties like post_status, post_mime_type, or even a valid ID. These \u201cghost attachments\u201d can wreak havoc on both the Grid View (which relies on AJAX and JSON) and the List View (which displays PHP warnings).<\/p>\n<h2>Common Symptoms of Broken Media Entries<\/h2>\n<p>Grid view shows no images (just a loading spinner or blank thumbnails)<br \/>\nList view shows dozens of errors like:<\/p>\n<p>[code_block]<br \/>\nWarning: Attempt to read property &#8220;ID&#8221; on null&#8230;<br \/>\nNotice: Function map_meta_cap was called incorrectly&#8230;<br \/>\n[\/code_block]<\/p>\r\n    <div class=\"bwh-in-article-ad\">\r\n      <ins class=\"adsbygoogle\"\r\n           style=\"display:block; text-align:center;\"\r\n           data-ad-client=\"ca-pub-4299086769596754\"\r\n           data-ad-slot=\"8699383760\"\r\n           data-ad-format=\"auto\"\r\n           data-full-width-responsive=\"true\"><\/ins>\r\n      <script>\r\n        (adsbygoogle = window.adsbygoogle || []).push({});\r\n      <\/script>\r\n    <\/div>\n<p>debug.log shows warnings from class-wp-media-list-table.php, post.php, or template.php<\/p>\n<p>These issues are typically caused by incomplete attachment posts left behind from failed imports, plugin removals, database syncs, or corrupted uploads.<\/p>\n<h2>The Fix: Clean Up Invalid Attachments in Your Database<\/h2>\n<p>Here\u2019s a simple code snippet you can drop into your functions.php to automatically detect and delete invalid media attachments.<\/p>\n<h3>PHP Cleanup Script:<\/h3>\n<p>[code_block]<br \/>\nadd_action(&#8216;init&#8217;, function () {<br \/>\nif (!is_admin()) return;<\/p>\r\n    <div class=\"bwh-in-article-ad\">\r\n      <ins class=\"adsbygoogle\"\r\n           style=\"display:block; text-align:center;\"\r\n           data-ad-client=\"ca-pub-4299086769596754\"\r\n           data-ad-slot=\"8699383760\"\r\n           data-ad-format=\"auto\"\r\n           data-full-width-responsive=\"true\"><\/ins>\r\n      <script>\r\n        (adsbygoogle = window.adsbygoogle || []).push({});\r\n      <\/script>\r\n    <\/div>\n<p>global $wpdb;<\/p>\n<p>$attachments = $wpdb-&gt;get_results(&#8221;<br \/>\nSELECT ID FROM {$wpdb-&gt;posts}<br \/>\nWHERE post_type = &#8216;attachment&#8217;<br \/>\n&#8220;);<\/p>\n<p>$deleted = 0;<\/p>\n<p>foreach ($attachments as $attachment) {<br \/>\n$post = get_post($attachment-&gt;ID);<\/p>\n<p>if (<br \/>\nempty($post) ||<br \/>\nempty($post-&gt;ID) ||<br \/>\nempty($post-&gt;post_status) ||<br \/>\nempty($post-&gt;post_mime_type)<br \/>\n) {<br \/>\nwp_delete_post($attachment-&gt;ID, true);<br \/>\n$deleted++;<br \/>\n}<br \/>\n}<\/p>\n<p>echo &#8220;\u2705 Deleted $deleted invalid attachment(s). Please remove this cleanup code now.&#8221;;<br \/>\nexit;<br \/>\n});<\/p>\n<p>[\/code_block]<\/p>\n<p>Warning: It will break you site temporary but just remove the functions after you run it and you should be fine. The media libraries should be back on track.<\/p>\n<h2><\/h2>\n<h2>What This Script Does<\/h2>\n<ul>\n<li>Queries all media entries from wp_posts where post_type = &#8216;attachment&#8217;<\/li>\n<li>Loops through each attachment and verifies if:\n<ul>\n<li>The post exists and is valid<\/li>\n<li>It has a valid ID, post_status, and post_mime_type<\/li>\n<\/ul>\n<\/li>\n<li>If any field is missing, it deletes the attachment permanently (wp_delete_post() with true)<\/li>\n<li>Outputs how many broken entries were removed<\/li>\n<\/ul>\n<\/p>","protected":false},"excerpt":{"rendered":"<p>If you&#8217;re running a local WordPress site or recently migrated data and started seeing strange behavior in the Media Library\u2014like missing images, broken grid views, or a flood of PHP warnings\u2014you\u2019re not alone. Many developers encounter issues where attachments exist in the database but are missing essential properties like post_status, post_mime_type, or even a valid [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-54299","post","type-post","status-publish","format-standard","hentry","category-cms"],"_links":{"self":[{"href":"https:\/\/bookwormhead.com\/cms\/wp-json\/wp\/v2\/posts\/54299","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bookwormhead.com\/cms\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bookwormhead.com\/cms\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bookwormhead.com\/cms\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bookwormhead.com\/cms\/wp-json\/wp\/v2\/comments?post=54299"}],"version-history":[{"count":5,"href":"https:\/\/bookwormhead.com\/cms\/wp-json\/wp\/v2\/posts\/54299\/revisions"}],"predecessor-version":[{"id":54304,"href":"https:\/\/bookwormhead.com\/cms\/wp-json\/wp\/v2\/posts\/54299\/revisions\/54304"}],"wp:attachment":[{"href":"https:\/\/bookwormhead.com\/cms\/wp-json\/wp\/v2\/media?parent=54299"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bookwormhead.com\/cms\/wp-json\/wp\/v2\/categories?post=54299"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bookwormhead.com\/cms\/wp-json\/wp\/v2\/tags?post=54299"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}