0

I have the following snippet that allows me to auto generate the post title. I'd like to change the $post_type of the 'post_title' => $post_type . ' Job #' . $post_id line to some other post property like post_date and post_author.

function save_post_func_2134( $post_id ) {     
    $post_type = 'post'; //Change your post_type here     
    if ( $post_type == get_post_type ( $post_id ) ) {  //Check and update for the specific $post_type         
        $my_post = array(
            'ID'           => $post_id,
            'post_title' => $post_type . ' Job #' . $post_id //Construct post_title 
        );
         remove_action('save_post', 'save_post_func_2134'); //Avoid the infinite loop 
        // Update the post into the database
        wp_update_post( $my_post );              
    }
}
add_action( 'save_post', 'save_post_func_2134' );
1

The post object is passed by reference as the second parameter of the save_post action (do_action ( 'save_post', int $post_ID, WP_Post $post, bool $update ). You can use this post object to get the post date and author from.

You can try the following: (NOTE: The code is untested)

add_action( 'save_post', 'wpse_214927_alter_title', 10, 2 );
function wpse_214927_alter_title ( $post_id, $post_object )
{
    // Target only specific post type
    if (    'my_specific_post_type'       !== $post_object->post_type
         && 'my_other_specific_post_type' !== $post_object->post_type
    )
        return;

    // Remove the current action
    remove_action( current_filter(), __FUNCTION__ );

    $post_date      = $post_object->post_date;
    $format_date    = DateTime::createFromFormat( 'Y-m-d H:i:s', $post_date );
    $date_formatted = $format_date->format( 'Y-m-d' ); // Set correct to display here
    $post_author    = $post_object->post_author;
    $author_name    = get_the_author_meta( 'display_name', $post_author ); // Adjust as needed

    $my_post = [
        'ID' => $post_id,
        'post_title' => $author_name . ' Job ' . $date_formatted // Change as needed
    ];
    wp_update_post( $my_post );
}

Just an important note, you should add validation and sanitation where needed

  • Thanks, the post author and post ID are not showing in the title. The ' Job ' is showing. – Pete Jan 19 '16 at 12:01
  • Stupid me, totally forgot to adjust my action to accept the 2nd parameter. passed by reference. It should work now – Pieter Goosen Jan 19 '16 at 16:29
  • This was the result... "1 Job 2016-06-02 00:00:00". It's not showing "1" rather than the author ID rather than the username How would I remove the the time? – Pete Jan 20 '16 at 1:27
  • This is being applied to all posts, custom posts and pages. Can I only make it apply to a specific post-type? – Pete Jan 22 '16 at 12:05
  • What would be needed to restrict the above to 2 specific CPTs? – Pete Apr 18 '16 at 4:28
0

Use the function get_post_meta($post_id) to get the post meta data.

Hope this helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.