Newsletter Content Erased on Updates

A strangely blank newsletter post on the website brought this problem to my attention. When the newsletter custom content type is updated without a file selected for upload, the post content is erased. As this post type no longer supports the editor window, my guess is that the post content is never pulled from the database and displayed, so it’s just assumed to be empty on the update.

The first thing I tried was to force an insertion of the post content regardless of whether a file has been uploaded. However, this didn’t work. Although the post content did get updated correctly, the page seems to hang and finally just gives a server error. Not user friendly at all.

Examining the WordPress update and insert functions in detail led me to guess that it ends up in an infinite loop. wp_update_post() calls the save_post action. In my code, this calls wp_update_post(), which will then end up calling save_post again, and so on ad infinitum.

The solution I finally settled on was to modify the loop. Since the existing posts already use the post content to display the links, I kept that there for backward compatibility. However, if the post content is empty, the loop will get the children and generate the necessary HTML to display the download link.

Leave a comment