Posts created in a Custom Post Type are lost if published without a title

I searched around and the URL below is all I could find on this problem. If I publish a custom post type’s post without entering in a post title, it wipes the whole post’s info and says “published”. However, the post is nowhere to be seen. Looking into the database, I can see the post is created, but it’s set to auto draft. The post_name value is empty

Found this. Sounds like exactly what I’m experiencing
http://www.wptavern.com/forum/plugins-hacks/1851-custom-post-type-posts-published-without-title-lost.html

Any ideas on how to fix this? Or maybe someone can suggest a way of checking to see if a post title exists when the post it published, and display a error message to the user?

Update
Thanks to Manny’s solution below, I managed to make a few modifications from WordPress prompt checklist before publish? and fix the publish button so it doesn’t keep spinning when you close the alert box.

add_action('admin_head', 'post_title_check');
function post_title_check() { ?>

<script type="text/javascript">
    jQuery(document).ready(function() {
        jQuery('#post').submit(function() {

            if(jQuery('input[name="post_title"]').val() == '') {
                alert("Please input a title");  

               jQuery('#ajax-loading').hide();
               jQuery('#publish').removeClass('button-primary-disabled');
               return false;  
            }

        }); 
    });
</script>

1 Answer
1

The function that’s in charge of saving posts on the database (wp_insert_post) requires at the minimum a title and content:

http://codex.wordpress.org/Function_Reference/wp_insert_post

Edit: I created a quick and dirty jquery solution. Input this in your functions file or in a plugin:

<?php
add_action('admin_head', 'post_title_check');
function post_title_check() {
    ?><script type="text/javascript">
        jQuery(document).ready(function($) {
            $('input[name="save"]').click(function() {
                if($('input[name="post_title"]').val() ==='') {
                    alert("Please input a title");  
                    return false;
                }
            }); 
        });
    </script>'
    <?php   
}
?>

Basically checks to see if the text field is empty. If it is, gives you a message and prevents you from continuing. If it has something in the field, it submits with no issue.

Leave a Comment