default title for custom post types

I have some code to change the default title of posts but I would like to limit its application to specific post types. Something like:

add_filter('title_save_pre', 'bilby_title');
function bilby_title($title) {
if ( post_type = "post_type_1" or "post_type_2" .... ) {
    $title = date_i18n('g:i a l, F j, Y');
    return $title;
   }
}

I'm not sure how to code the conditional properly. Could you give me any suggestion?

Thanks - Mark.

  • Milan

    Hello @Mark,

    I hope you are having a good day and thanks for the question. :slight_smile:

    Actually first let me tell you about function which will give you current post type slug. get_post_type is the function you can use to currently processing post type slug. It will return you post type slug which used while registering post type. if you are not sure about what it was you can always dump data via "var_dump" function. :slight_smile:

    So here is modified version of your sample code snippet with get_post_type function. :slight_smile:

    add_filter('title_save_pre', 'bilby_title');
    function bilby_title($title) {
    if ( get_post_type() = "post_type_1" ||  get_post_type() == "post_type_2" .... ) {
        $title = date_i18n('g:i a l, F j, Y');
        return $title;
       }
    }

    I hope this helps you.

    Please let me know if there is anything else I can help you with.

    Cheers,
    Milan Savaliya.

  • Mark

    Hi Milan,
    Thanks for the help! Something is not quite right. I tried this"

    add_filter('title_save_pre', 'my_title');
    
    function my_title($title) {
    	if ( get_post_type() == "annual-srm-record"  ) {
        	$title = date_i18n('g:i a l, F j, Y');
        	return $title;
    	}
        	}

    This code causes many of the meta boxes from the add new post screen to disappear and the following message to pop up at the top of the screen:

    "You are currently editing the page that shows your latest posts."

    Then when clicking publish I get:

    "You are not allowed to edit this post."

    This code works fine without the conditional, however it is applied to all post types.

    Can you see anything I have done wrong?

    Thanks again for the help,
    Mark

  • Mark

    ...actually it is not really working at all. It was not the other plugin. The code below works:

    function my_title($title) {
        $title = date_i18n('g:i a l, F j, Y');
        return $title;
        }
    add_filter('title_save_pre', 'my_title');

    ...but it renames all titles of all post types to the current date and time. Even when updating an existing post. Introducing the conditional statement below seems to break many things.

    function my_title($title) {
        if ( get_post_type() === 'annual-srm-record' ){
        $title = date_i18n('g:i a l, F j, Y');
        return $title;
        }
        }
    add_filter('title_save_pre', 'my_title');

    Instead I have used save_post below and it seems to be working:

    // set title
    function set_custom_title ($post_id) {
        if ( $post_id == null || empty($_POST) )
            return;
    
        if ( !isset( $_POST['post_type'] ) || $_POST['post_type']!='annual-srm-record' )
            return; 
    
        if ( wp_is_post_revision( $post_id ) )
            $post_id = wp_is_post_revision( $post_id );
    
        global $post;
        if ( empty( $post ) )
            $post = get_post($post_id);
    
        if ($_POST['post_title']='Auto Draft') {
            global $wpdb;
            $title = date_i18n('g:i a l, F j, Y');
            $where = array( 'ID' => $post_id );
            $wpdb->update( $wpdb->posts, array( 'post_title' => $title ), $where );
        }
    }
    add_action('save_post', 'set_custom_title', 12 );

    I'm not sure why the title_save_pre didn't work.

    Thanks again for the help,
    Mark

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.