• Resolved midimatt

    (@midimatt)


    I am creating a custom post type that I built a custom initial post creation process for. I programatically set the title, content, etc. I also set the post_status to “auto-draft” so that if the user backs out of the process, the post will be deleted in time. When I thereafter go to edit the post, the title does not appear in the post editing interface. I can see that the title exists when viewing the post in the database, but the Title field is not populated when entering the edit screen.

    If I manually change the post_status to draft by modifying the field in the database, the title shows up fine in the editing interface.

    It seems that other post information is retained and displayed when editing an auto-draft – just not the post_title.

    Is there a way to have the Post Title field populate with its value as stored in the database when editing an auto-draft?

    Note: This is being developed off-line, so I cannot provide a URL, however I can provide code if requested.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    The edit form code intentionally blanks auto draft titles. There’s no clean way to prevent it from doing so. You might be able to put it back after it is blanked. Hook an action like ‘edit_form_top’ and check if the passed post object is the same as the global $post object. If it is, you could fetch the title again from the DB and restore the title to the global $post object, whose post_title property is used to populate the form’s title field. (untested)

    If your only purpose is so abandoned posts are auto deleted, I don’t think relying upon auto drafts is an optimal plan. I’d suggest saving regular drafts with some unique trait that makes them easy to query for. Then have a scheduled event run on occasion that deletes all such posts older than X amount of time. Use one of the post transition actions or ‘wp_insert_post_data’ to unset this unique trait when posts are saved by a user.

    Thread Starter midimatt

    (@midimatt)

    I like your idea there in the second paragraph. I’ll give that a shot. Thank you very much for the response!

    Thread Starter midimatt

    (@midimatt)

    What I ended up doing is registering a custom post_status and using that (as the unique trait you mentioned), then added a scheduled event that removes such posts created over a day ago daily. Seems to work like a charm! Thanks again!

    Moderator bcworkz

    (@bcworkz)

    Nice! You’re welcome.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Title Vanishes when editing Auto Draft’ is closed to new replies.