Wordpress Media Library Wont Load - Calls admin-ajax.php continuously

I recently came across a strange wordpress issue wherein the media library did not load and multiple calls were being made by the CMS to admin-ajax.php. Now the load of the server was rising as well as these calls seemed to be fruitless. Quite confident that someone somewhere would have resolved the issue , I began my search only to find numerous closed posts on the Wordpress.org forum and unanswered questions on Stackoverflow.

I took a deep dive first understanding the nature of the issue and to my surprise the issue had something to do with moving the wordpress site from one domain or server to another in most cases. The second bug was that although you restored from backup the issue wouldn't go away. Most of these installations were quite large in terms of number of posts and something as naive as WP Clone was not suitable. I call WP Clone naive in comparison to Joomla's Akeeba backup and clone manager.(Joomla's got its own Santa's)

 

I started looking at the database, only to find numerous posts with id = 0.......

WHEW ! it hit me then...that they could be lacking indices.

What is supposed to happen when you write a post or upload a new image is that Wordpress cleverly uploads it and treats it as a post even though it is an image. Then it writes a new post to the database and because it lacked its index it got posted as post '0' . Now Wordpress is even smarter than itself being penny wise with database tables and makes about 3 to 4 meta's for each post. It then checks the post id and because it was 0 treats it as empty and does not post the metas . Now this causes a bug in wordpress with the result that numerous incessant calls are made to admin-ajax.php like it were loading Pinterest in its entirety. The good part is wordpress still works although it lacks its indices and primary keys  in a botched transfer.

 

So how do you resolve this issue. Phpmyadmin could be a very useful tool. Compare the tables of a new wordpress siite and yours (just open a subdomain and installa fresh temp installation).

See which tables need to be refurbished with index and primary key information and dont forget you need to delete the '0' in post id. Simply export the structure alone using phpmyadmin and weed out the tables that have previously been updated.

Your phpmyadmin effort will look something like this

DELETE FROM  `wp_posts` WHERE  `ID` =0;
DELETE FROM  `wp_comments` WHERE  `comment_ID` =0;
DELETE FROM  `wp_options` WHERE  `option_id` =0;
DELETE FROM  `wp_postmeta` WHERE  `meta_id` =0;

-- Indexes for dumped tables
-- Indexes for table `wp_commentmeta`
ALTER TABLE `wp_commentmeta`
  ADD PRIMARY KEY (`meta_id`), ADD KEY `comment_id` (`comment_id`), ADD KEY `meta_key` (`meta_key`);
-- Indexes for table `wp_comments`
--
ALTER TABLE `wp_comments`
  ADD PRIMARY KEY (`comment_ID`), ADD KEY `comment_post_ID` (`comment_post_ID`), ADD KEY `comment_approved_date_gmt` (`comment_approved`,`comment_date_gmt`), ADD KEY `comment_date_gmt` (`comment_date_gmt`), ADD KEY `comment_parent` (`comment_parent`), ADD KEY `comment_author_email` (`comment_author_email`(10));
-- Indexes for table `wp_links`
ALTER TABLE `wp_links`
  ADD PRIMARY KEY (`link_id`), ADD KEY `link_visible` (`link_visible`);
-- Indexes for table `wp_options`
ALTER TABLE `wp_options`
  ADD PRIMARY KEY (`option_id`), ADD UNIQUE KEY `option_name` (`option_name`);
-- Indexes for table `wp_postmeta`
ALTER TABLE `wp_postmeta`
  ADD PRIMARY KEY (`meta_id`), ADD KEY `post_id` (`post_id`), ADD KEY `meta_key` (`meta_key`);
-- Indexes for table `wp_posts`
--
ALTER TABLE `wp_posts`
  ADD PRIMARY KEY (`ID`), ADD KEY `post_name` (`post_name`), ADD KEY `type_status_date` (`post_type`,`post_status`,`post_date`,`ID`), ADD KEY `post_parent` (`post_parent`), ADD KEY `post_author` (`post_author`);
-- Indexes for table `wp_relevanssi`
ALTER TABLE `wp_relevanssi`
  ADD UNIQUE KEY `doctermitem` (`doc`,`term`,`item`), ADD KEY `terms` (`term`(20)), ADD KEY `relevanssi_term_reverse_idx` (`term_reverse`(10)), ADD KEY `docs` (`doc`), ADD KEY `typeitem` (`type`,`item`);

-- Indexes for table `wp_relevanssi_log`
ALTER TABLE `wp_relevanssi_log`
  ADD UNIQUE KEY `id` (`id`);

--
-- Indexes for table `wp_relevanssi_stopwords`
--
ALTER TABLE `wp_relevanssi_stopwords`
  ADD UNIQUE KEY `stopword` (`stopword`);
-- Indexes for table `wp_slim_browsers`
ALTER TABLE `wp_slim_browsers`
  ADD PRIMARY KEY (`browser_id`), ADD UNIQUE KEY `unique_browser` (`browser`,`version`,`platform`,`css_version`,`type`);
-- Indexes for table `wp_slim_content_info`
ALTER TABLE `wp_slim_content_info`
  ADD PRIMARY KEY (`content_info_id`), ADD UNIQUE KEY `unique_content_info` (`content_type`(20),`category`(20),`author`(20),`content_id`);
-- Indexes for table `wp_slim_outbound`
ALTER TABLE `wp_slim_outbound`
  ADD PRIMARY KEY (`outbound_id`), ADD KEY `odt_idx` (`dt`), ADD KEY `fk_wp_id` (`id`);
-- Indexes for table `wp_slim_screenres`
ALTER TABLE `wp_slim_screenres`
  ADD PRIMARY KEY (`screenres_id`), ADD UNIQUE KEY `unique_screenres` (`resolution`,`colordepth`,`antialias`);
-- Indexes for table `wp_slim_stats`
--
ALTER TABLE `wp_slim_stats`
  ADD PRIMARY KEY (`id`), ADD KEY `dt_idx` (`dt`), ADD KEY `fk_wp_browser_id` (`browser_id`), ADD KEY `fk_wp_content_info_id` (`content_info_id`);

--
-- Indexes for table `wp_slim_stats_archive`
--
ALTER TABLE `wp_slim_stats_archive`
  ADD PRIMARY KEY (`id`), ADD KEY `dt_idx` (`dt`), ADD KEY `fk_wp_browser_id` (`browser_id`), ADD KEY `fk_wp_content_info_id` (`content_info_id`);

--
-- Indexes for table `wp_ssubscribe_app`
--
ALTER TABLE `wp_ssubscribe_app`
  ADD PRIMARY KEY (`eemail_app_pk`);

--
-- Indexes for table `wp_terms`
--
ALTER TABLE `wp_terms`
  ADD PRIMARY KEY (`term_id`), ADD KEY `name` (`name`), ADD KEY `slug` (`slug`);

--
-- Indexes for table `wp_term_relationships`
--
ALTER TABLE `wp_term_relationships`
  ADD PRIMARY KEY (`object_id`,`term_taxonomy_id`), ADD KEY `term_taxonomy_id` (`term_taxonomy_id`);

--
-- Indexes for table `wp_term_taxonomy`
--
ALTER TABLE `wp_term_taxonomy`
  ADD PRIMARY KEY (`term_taxonomy_id`), ADD UNIQUE KEY `term_id_taxonomy` (`term_id`,`taxonomy`), ADD KEY `taxonomy` (`taxonomy`);

--
-- Indexes for table `wp_usermeta`
--
ALTER TABLE `wp_usermeta`
  ADD PRIMARY KEY (`umeta_id`), ADD KEY `user_id` (`user_id`), ADD KEY `meta_key` (`meta_key`);

--
-- Indexes for table `wp_users`
--
ALTER TABLE `wp_users`
  ADD PRIMARY KEY (`ID`), ADD KEY `user_login_key` (`user_login`), ADD KEY `user_nicename` (`user_nicename`);

--
-- Indexes for table `wp_wpclone`
--
ALTER TABLE `wp_wpclone`
  ADD PRIMARY KEY (`id`);

--
-- Indexes for table `wp_wp_rp_tags`
--
ALTER TABLE `wp_wp_rp_tags`
  ADD KEY `post_id` (`post_id`), ADD KEY `label` (`label`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `wp_commentmeta`
--
ALTER TABLE `wp_commentmeta`
  MODIFY `meta_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=3090;
--
-- AUTO_INCREMENT for table `wp_comments`
--
ALTER TABLE `wp_comments`
  MODIFY `comment_ID` bigint(20) unsigned NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=1002;
--
-- AUTO_INCREMENT for table `wp_links`
--
ALTER TABLE `wp_links`
  MODIFY `link_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT;
--
-- AUTO_INCREMENT for table `wp_myarcadegames`
--
ALTER TABLE `wp_myarcadegames`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=28106;
--
-- AUTO_INCREMENT for table `wp_options`
--
ALTER TABLE `wp_options`
  MODIFY `option_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=36721;
--
-- AUTO_INCREMENT for table `wp_postmeta`
--
ALTER TABLE `wp_postmeta`
  MODIFY `meta_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=438658;
--
-- AUTO_INCREMENT for table `wp_posts`
--
ALTER TABLE `wp_posts`
  MODIFY `ID` bigint(20) unsigned NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=48932;
--
-- AUTO_INCREMENT for table `wp_relevanssi_log`
--
ALTER TABLE `wp_relevanssi_log`
  MODIFY `id` bigint(9) NOT NULL AUTO_INCREMENT;
--
-- AUTO_INCREMENT for table `wp_slim_outbound`
--
ALTER TABLE `wp_slim_outbound`
  MODIFY `outbound_id` int(10) unsigned NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=11;
--
-- AUTO_INCREMENT for table `wp_slim_screenres`
--
ALTER TABLE `wp_slim_screenres`
  MODIFY `screenres_id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=98;
--
-- AUTO_INCREMENT for table `wp_slim_stats`
--
ALTER TABLE `wp_slim_stats`
  MODIFY `id` int(10) unsigned NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=1412;
--
-- AUTO_INCREMENT for table `wp_slim_stats_archive`
--
ALTER TABLE `wp_slim_stats_archive`
  MODIFY `id` int(10) unsigned NOT NULL AUTO_INCREMENT;
--
-- AUTO_INCREMENT for table `wp_ssubscribe_app`
--
ALTER TABLE `wp_ssubscribe_app`
  MODIFY `eemail_app_pk` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=2;
--
-- AUTO_INCREMENT for table `wp_terms`
--
ALTER TABLE `wp_terms`
  MODIFY `term_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=882;
--
-- AUTO_INCREMENT for table `wp_term_taxonomy`
--
ALTER TABLE `wp_term_taxonomy`
  MODIFY `term_taxonomy_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=892;
--
-- AUTO_INCREMENT for table `wp_usermeta`
--
ALTER TABLE `wp_usermeta`
  MODIFY `umeta_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=20652;
--
-- AUTO_INCREMENT for table `wp_users`
--
ALTER TABLE `wp_users`
  MODIFY `ID` bigint(20) unsigned NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=168