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 Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s