WordPress.org

Forums

Fix Auto Draft Title, use custom field instead (5 posts)

  1. jonnyplow
    Member
    Posted 4 years ago #

    I created a post type, and then allowed only the editor, page-attributes, revisions, and comments for the page. I intentionally left out title. I then have a metabox with some fields. After entering in the metabox fields and publishing, I notice the title is actually "auto draft" when I look at the single page or the post listings for it.

    I don't want to use the standard title box to enter the title either, I actually want to use one of the custom fields to replace it. But I don't just want to mask it. I actually want to have the custom field saved as the actual title for the post also. This way when I look at the single page as well as the list of posts, it appears correctly there. I just can't seem to find a solution.

  2. jonnyplow
    Member
    Posted 4 years ago #

    This stuff was originally posted here.

    But I just tweaked it to work for 1 custom field to replace the title. It works perfect (thus far)!!!

    add_filter('title_save_pre', 'save_title');
    function save_title($my_post_title) {
            if ($_POST['post_type'] == 'post_type_name') :
              $new_title = $_POST['custom_field_name'];
              $my_post_title = $new_title;
            endif;
            return $my_post_title;
    }
    
    add_filter('name_save_pre', 'save_name');
    function save_name($my_post_name) {
            if ($_POST['post_type'] == 'post_type_name') :
              $new_name = $_POST['custom_field_name'];
              $my_post_name = $new_name;
            endif;
            return $my_post_name;
    }
  3. mcmasterp
    Member
    Posted 4 years ago #

    this doesn't work for me but I can't figure out why. it seems like the right way to do it. but when I hit save it doesn't save the post. it also screws up my permalinks.

  4. antonymken
    Member
    Posted 3 years ago #

    try this

    the code below sets the value of the hidden field, to the value of a custom field of your choice.
    this tricks wordpress into saving the hidden field as the title :)

    <div id="titlewrap">
    <input type="hidden" name="post_title" size="30" tabindex="1" value="" id="title" autocomplete="off" />
    </div>

    <script type="text/javascript">
    $( '#customfieldofchoice' ).blur( function () {
    $( '#title' ).val($( '#customfieldofchoice' ).val());
    });
    </script>

  5. billybitmap
    Member
    Posted 3 years ago #

    @jonnyplow: Thank you. This works great for me when I'm updating a custom post.

    However, when I save a post for the first time the title doesn't get saved. Is there another hook I need to use for triggering the function on a first save?

Topic Closed

This topic has been closed to new replies.

About this Topic