skip to Main Content

How can I stop my custom posts saving with the title ‘Auto Draft’? I’ve tried the code below to save my Book_Title custom field as the post-title but to no success:

if(isset($_POST['Book_Title'])){
    update_post_meta($post_id,'post_title', sanitize_text_field($_POST['Book_Title']));

    } else {
    delete_post_meta( $post_id, 'Book_Title' );
    }

3

Answers


  1. please you test for stop Auto Draft :
    define( ‘WP_POST_REVISIONS’, false ); //in wp-config.php

    if you use metabox :

    1-

     function add_price_post_meta_box( $post ){ //use get_post_meta} 
    

    2-

    function add_meta_box_price( $post_type, $post ){ 
    
    add_meta_box(
        'meta_box_price', 
    
        'price',
    
        'add_price_post_meta_box',//for 1
    
        'post',
    
        'normal',
    );
    

    }

    3-

    function save_meta_box_price($post_id){//use update or delete post meta}
    

    4-

    add_action( 'add_meta_boxes','add_meta_box_price', 10, 2 );//call function 2 
    
     add_action( 'save_post', 'save_meta_box_price' ); //for save call function 3
    
      
      
    
    Login or Signup to reply.
  2. If you are simply trying to change the default post_title WordPress uses when creating an auto-draft for your custom post type, the sample code below should work when added to a plugin or child theme. The code will only change the default Auto Draft post title for the identified custom post type while continuing to use "Auto Draft" for other post types. If you are trying to accomplish more than this, please explain further by updating your question or responding with a comment.

    Like many WordPress hooks, the title_save_pre filter hook does not yet have a dedicated documentation page on wordpress.org.

    If you are not familiar with creating your own plugin, you can refer to the WordPress Plugin Handbook and WordPress Plugin Basics.

    Sample code

    function change_auto_draft_default_title( $title ) {
      $NEW_TITLE = 'New Default Title';
      $CUSTOM_POST_TYPE = 'your_custom_post_type';
    
      $post = get_post();
      $post_type = $_GET['post_type'];
    
      if ( is_null( $post ) && 'Auto Draft' == $title && $CUSTOM_POST_TYPE == $post_type ) {
        $title = $NEW_TITLE;
      }
    
      return $title;
    }
    add_filter( 'title_save_pre', 'change_auto_draft_default_title', 10, 1 );
    
    Login or Signup to reply.
  3. You can add this code in config file as per your need.

    define( 'AUTOSAVE_INTERVAL',    3600 );     // autosave 1x per hour
    define( 'WP_POST_REVISIONS',    false );    // no revisions
    define( 'DISABLE_WP_CRON',      true );
    define( 'EMPTY_TRASH_DAYS',     7 );        // one week
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search