• Resolved Pete

    (@perthmetro)


    How would I apply this function to a specific post-type only, let’s say CPT XYZ?

    add_action( 'save_post', 'wpse_214927_alter_title', 10, 2 );
    function wpse_214927_alter_title ( $post_id, $post_object )
    {
        // 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 );
    }
Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    This topic is marked as resolved. It lacks an answer, so I’ll provide one, whether you still need it or not ??

    Wrap all of the function’s code in a conditional like so:

    if ('my_post_type' == $post_object->post_type ) {
      // code remove_action through wp_update_post here
    }

    The forum abhors topics with no replies ??

    Thread Starter Pete

    (@perthmetro)

    I tried to reply to it and mark it resolved soon after I posted it but my posts keep on getting marked as spam unfortunately so I can’t reply etc.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How to apply this 'auto post title' function to a specific post-type only’ is closed to new replies.