Set wordpress post title to date of creation

I want to set wordpress post title to date of creation (or anything else unique) for all posts of custom post type 'invoices'.

Essentially these post types just don't need a title, and asking the client to enter one is just extra work for nothing. So what I want is that when the client clicks save it just gives the posts a title and doesn't throw back the error due to no title.

  • Ash
    • WordPress Hacker

    Hello Willi

    Please try the following code:

    add_action( 'wp_insert_post_data', function( $data, $postarr ) {
    	if( 'invoices' != $data['post_type'] ) return;
    
    	$data['post_title'] = $data['post_title'] == '' ? $data['post_date'] : $data['post_title'];
    
    	return $data;
    }, 99, 2 );

    You can use this code in your child theme's functions.php if the theme is not changed. Otherwise mu-plugin is the best option. To create a mu-plugin, go to wp-content/mu-plugins folder. If there is no mu-plugins folder then, create one. Now, inside the mu-plugins folder create file with name anything.php (make sure file extension is .php). Now start with a <?php tag and then put the above code.

    This code will work if you leave the title field empty and then save. Only empty values will be replaced with the post creation date and time.

    Hope it helps! Please feel free to ask more questions if you have any.

    Have a nice day!

    Cheers,
    Ash

  • Willi
    • Flash Drive

    Hi Ash,

    I thought I had replied to this already but must have lost it somehow. I tried the above code as an MU plugin (autoinvoicetitle.php) and it didn't work. Now that I have sent you my login details do you think you could have a look?

    Cheers
    Willi

  • Willi
    • Flash Drive

    Hi Ash,
    I was speaking to someone here at WPMUDEV who looked for the name of the custom post type and came up with "invoices". I wasn't sure how to check what the custom post type is created by wp-invoice plugin, are you able to do that?
    Cheers

  • Ash
    • WordPress Hacker

    Hello there

    If you are talking about DOMAIN.com.au/wp-admin/admin.php?page=wpi_page_manage_invoice page, then this is not usual custom post type. It looks like an admin page that just uses the custom post type screen layout. Not sure if in the backend it deals with a post type, but that won't work.

    So, I have applied a different code there, please check now. Here is the used code:

    add_action( 'admin_head', function() {
        if( isset( $_REQUEST['page'] ) && 'wpi_page_manage_invoice' == $_REQUEST['page'] )
        {
            ?>
            <script type="text/javascript">
                jQuery(function($){
                    if( $('#title').val() == '' )
                    {
                        var currentdate = new Date();
                        var datetime = currentdate.getDate() + "/"
                            + (currentdate.getMonth()+1)  + "/"
                            + currentdate.getFullYear() + " @ "
                            + currentdate.getHours() + ":"
                            + currentdate.getMinutes() + ":"
                            + currentdate.getSeconds();
                        $('#title').val( datetime );
                    }
                });
            </script>
            <?php
        }
    } );

    Hope it helps! Please feel free to ask more questions if you have any.

    Have a nice day!

    Cheers,
    Ash

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.