1

I am trying to work out how to change details of a post after its status has changed.

I would like to change the title of the post by appending the post id number to it. Here is the code that I'm working with now, but it doesn't change the post's title:

function update_post_info( $post_id, $post, $update ) {

// Stop anything from happening if revision
if ( wp_is_post_revision( $post_id ) ) return;

//get post type
   $post_type = get_post_type($post_id);

// If this isn't a custom post, don't update it.
// if ( "cbre_access_form" != $post_type ) return;

     //run codes based on post status
      $post_status = get_post_status();
      if ( $post_status != 'draft' ) 
      {
         if ( isset( $_POST['post_title'] ) ) {
           //stuck on this part not changing post title
           $ppt = 'test title - '.$post_id;
           update_post_meta( $post_id, 'post_title', $ppt );
         }
      }
}
add_action( 'save_post', 'update_post_info', 10, 3 );
1

A post's title (post_title) is not saved in meta data; it's a field within the post table.

Here's an updated version of your original code.

  • Infinite loop is prevented by removing and then readding the wpse246957_update_post_info callback.

  • Post title is successfully saved with the suffix - $post_id A check is in place to prevent the suffix from being re-added if it has already been added.

add_action( 'save_post', 'wpse246957_update_post_info', 10, 3 );
function wpse246957_update_post_info( $post_id, $post, $update ) {

    // Stop anything from happening if revision
    if ( wp_is_post_revision( $post_id ) ) {
        return;
    }

    // unhook this function so it doesn't loop infinitely
    remove_action( 'save_post', 'wpse246957_update_post_info' );

    // get post type
    $post_type = get_post_type( $post_id );

    // If this isn't a custom post, don't update it.
    // if ( "cbre_access_form" != $post_type ) return;

    // run codes based on post status
    $post_status = get_post_status();
    if ( $post_status != 'draft' )  {
        if ( isset( $_POST['post_title'] ) ) {

            $suffix = ' - ' . $post_id;
            if ( ! preg_match( '/' . preg_quote( $suffix, '/' ) . '$/', $_POST['post_title'] ) ) {
                wp_update_post( [
                        "ID"         => $post_id,
                        "post_title" => $_POST['post_title'] . $suffix,
                ] );            
            }
        }
    }

    // re-hook this function
    add_action( 'save_post', 'wpse246957_update_post_info', 10, 3 );
}

Personally, I'd probably append the suffix before outputting the title in your template file or wherever you are outputting the title because there could be some edge case with the approach above when it comes to re-saving the post title.

  • I am getting the following : Missing argument 2 for wpse246957_update_post_info(), Missing argument 3 for wpse246957_update_post_info() Cannot modify header information - headers already sent by But after going to the posts it has added the number. – Sam Miller Nov 22 '16 at 0:02
  • Woops! I fixed the re-hooking of wpse246957_update_post_info so that the priority and parameter numbers are specified. That should clear up the warning. – Dave Romsey Nov 22 '16 at 0:07
  • Thanks that works! only issue I found is it stops my custom title changing unless post is saved twice as I am using acf and picking two fields to save as the title in the first instance. – Sam Miller Nov 22 '16 at 0:14
  • Happy to help, Sam! I'm not familiar with using ACF, but I really do suggest appending the ` - $post_id` suffix during output to avoid these kinds of edge cases. One route you could take would be to use the the_title filter. You could add conditionals in the filter so that you're only affecting the desired posts. Alternatively, you could just concatenate the suffix directly in the template file. – Dave Romsey Nov 22 '16 at 0:19
  • 1
    Great code, thanks. Clever to unhook and re-hook. – Eric Jun 11 '18 at 8:18
0

post_title saved in the posts the table as post_title not in the post_meta; Also you have to remove the save_post hook and then add because wp_update_post results in save_post being fired and it will create the infinite loop.


function update_post_info( $post_id, $post, $update ) {

// Stop anything from happening if revision
if ( wp_is_post_revision( $post_id ) ) return;

//get post type
   $post_type = get_post_type($post_id);

// If this isn't a custom post, don't update it.
// if ( "cbre_access_form" != $post_type ) return;

     //run codes based on post status
      $post_status = $post->post_status;
      if ( $post_status != 'draft' ) 
      {
     if ( isset( $_POST['post_title'] ) ) {
       $my_post = array(
        'ID'           => $post_id,
        'post_title'   => $_POST['post_title'].' - '.$post_id,
       );
        if ( ! wp_is_post_revision( $post_id ) ) {

            // unhook this function so it doesn't loop infinitely
            remove_action('save_post', 'update_post_info');

            // update the post, which calls save_post again
            wp_update_post( $my_post );

            // re-hook this function
            add_action('save_post', 'update_post_info');
        }
       }
      }
}
add_action( 'save_post', 'update_post_info', 10, 3 );


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.