title_save_pre on post publish

I’m trying to build the title of a custom post using the values within post custom fields. The problem is that using title_save_pre only executes when I update the post, not when I create it.

When I create the post I get the title : (no title)
If I edit and save I get the right title.

I need to get the right title in backend.

This is my code:

function update_phone_title($title) {

global $post;

$type = get_post_type($post->ID);

if ( 'phone' == $type) {
    $title = get_post_meta($post->ID, 'manufacturer', TRUE);
    $title .= ' '.get_post_meta($post->ID, 'model', TRUE);
    $title .= ' '.get_post_meta($post->ID, 'memory', TRUE);
} 

return $title;
}
add_filter ('title_save_pre', 'update_phone_title');

FIXED using wp_insert_post_data .I did it with this code:

function filter_handler( $data , $postarr )
{
  global $post;
  $id = $post->ID;

  if('phone' == $data['post_type'] && isset($data['post_type']))
    if($id) {
        $title = $_POST['fields']['field_4fd2897fc0bfe']. ' ' . $_POST['fields']['field_4fd2897fc2328'] . ' ' .$_POST['fields']['field_4fd2897fc273a'];
        $data['post_title'] = $title;
    }
  return $data;
}

add_filter( 'wp_insert_post_data' , 'filter_handler' , '99', 2 );

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

The filter title_save_pre runs before the post is written to the database, and also before any of its post meta is saved to the database. So get_post_meta at this point will be blank for the first run, and always return ‘old’ data.

To correct this you’ll want to hook into save_post and update the post from there. save_post runs after the post has been saved to the database, and the in-built ‘custom fields’ have been saved.

To do this you can use wp_update_post. However, you’ll need to be careful:

When executed by an action hooked into save_post (e.g. a custom metabox), wp_update_post() has the potential to create an infinite loop. This happens because (1) wp_update_post() results in save_post being fired and (2) save_post is called twice when revisions are enabled (first when creating the revision, then when updating the original post—resulting in the creation of endless revisions).

If you must update a post from code called by save_post, make sure to verify the post_type is not set to ‘revision’ and that the $post object does indeed need to be updated.

One way round this is to remove the save_post callback from the hook inside the callback, as demonstrated here.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x