Assign Default Title to Custom Post Types

I would like to remove the Title field from my Custom Post Input Page. What is the code for a function that will assign a Title to a Custom Post Type in a numbered fashion (e.g., Coupon1, Coupon2, Coupon3, etc…:wink:?

  • Barry
    • DEV MAN’s Mascot

    Ok, you could try this:

    function add_custom_title( $data, $postarr ) {

    if($data['post_type'] == 'myposttype') {
    if(empty($data['post_title']) {
    $data['post_title'] = 'my post title';
    }
    }

    return $data;

    }

    add_filters('wp_insert_post_data', 'add_custom_title', 10, 2 );

  • Barry
    • DEV MAN’s Mascot

    With count

    function add_custom_title( $data, $postarr ) {

    if($data['post_type'] == 'myposttype') {
    if(empty($data['post_title']) {

    $count = get_option('myposttypecount', 1);

    $data['post_title'] = 'my post title ' . $count++;

    update_option( 'myposttypecount', (int) $count );
    }
    }

    return $data;

    }

    add_filters('wp_insert_post_data', 'add_custom_title', 10, 2 );

  • AC
    • Flash Drive

    Hey Barry,

    Thank you so much for contributing. I’ve fiddled with the code you’ve pasted a few times over the past month, but I still can’t get it to work. I’ll paste what I have here and if anyone can chime in I would really appreciate it…

    The Targeted Post Type: mobile_coupons

    The Default Title to Set: Mobile Coupon (and the corresponding number)

    Here is Barry’s Code with my information filled into it:

    function add_custom_title( $data, $postarr ) {

    if($data['post_type'] == 'mobile_coupons') {
    if(empty($data['post_title']) {

    $count = get_option('mobile_couponscount', 1);

    $data['post_title'] = 'Mobile Coupon' . $count++;

    update_option( 'mobile_couponscount', (int) $count );
    }
    }

    return $data;

    }

    add_filters('wp_insert_post_data', 'add_custom_title', 10, 2 );

  • Philip John
    • DEV MAN’s Apprentice

    Hiya,

    Just checking if it was eventually resolved in another thread? Or by yourself separately to us? Or by us over email with you? Or using our live support?

    If so, no need to reply, that’s great news.

    If not, could you let us know by re-opening this topic, and we’ll get onto it and helping you out asap!

    Otherwise, happy days, glad you got it sorted :slight_smile:

    Thanks!

    Phil

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.