Support » Developing with WordPress » Auto-Generate Post Title

  • I am trying to find a way to auto-generate a post title when using the ‘status’ post format. I would like the title to auto-generate to “Status Update – <time> – <date>”. Any advice on if this is possible and how to make it happen would be appreciated.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Add a function in the functions.php file witin your Child Theme –

    function set_custom_title( $value, $post_id ) {
    $label = 'Status Update';
    
    $date = date("Ymd");
    $date =  date("Y-m-d", strtotime($date));
    $timeis = date("h:i:sa");
    $title = $label . ' - ' . $timeis . ' - ' . $date;
    $post_slug = sanitize_title_with_dashes ($title,'','save');
    $post_slugsan = sanitize_title($post_slug);
    $value['post_title'] = $title;
    $value['post_name'] = $post_slugsan;
    return $value;
    }
    add_filter( 'wp_insert_post_data' , 'set_custom_title' , '10', 2 );

    This will generate the new Post Title on Save.
    https://ibb.co/cJ1VBdG

    Thread Starter Jim Lunsford

    (@jimlunsford)

    Thank you for your reply.

    Is there a way to only auto-generate a title when the post format that is selected is status?

    Also when I tested it the time was off by five hours from my time zone and the date displayed as 1970-01-01.

    > the post format that is selected is status?

    I don’t know what this means.
    You man you have a *custom post type* that is named ‘status’ ?

    You can just add a conditional loop, you may be able to use is_singular :
    https://developer.wordpress.org/reference/functions/is_singular/

    or you may have to get the post types first, then check against it :
    https://developer.wordpress.org/reference/functions/get_post_types/

    Do you have your WP settings set properly :
    https://www.wpbeginner.com/beginners-guide/how-to-change-date-and-time-format-in-wordpress/
    Or, you can check with your host.

    Thread Starter Jim Lunsford

    (@jimlunsford)

    This is what I mean by post format:
    https://wordpress.org/support/article/post-formats/

    I’m fairly certain that my time is configured properly. Is it possible that the server is not?

    Can you ftp or ssh over to the server and check the timestamp on your files ?

    The date is likely returning the unix epoch.

    Change to date variable from this :
    $date = date(“Y-m-d”, strtotime($date));
    to this :
    $date = date(“Y-m-d”);

    For time, use your locale – i.e.
    date_default_timezone_set(“America/New_York”);
    $timeis = date(“h:i:sa”);

    Moderator bcworkz

    (@bcworkz)

    The $post_id parameter collected in corrinarusso’s code isn’t really the post ID, it’s an array of slashed but otherwise unmodified post data (see here). I believe the post format selected is in there somewhere, probably under “tax_input” since post formats are actually taxonomy terms. If it’s in there, you can conditionally only set titles this way when the “status” format term is assigned.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Auto-Generate Post Title’ is closed to new replies.