Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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 );
share|improve this question
    
I would use the save_post hook instead. You should also perform some nonce and permission checks. wp_insert_post_data (like save_post) is triggered on occasions when the post is created or edited outside of the 'edit post' screen, where your data in $_POST['fields'] is not present. –  Stephen Harris Jun 10 '12 at 14:29
add comment

1 Answer

up vote 1 down vote accepted

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.

share|improve this answer
    
I added my fix to the initial question. –  Bdesign Jun 10 '12 at 12:09
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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