I have created the following hook in my functions.php

add_filter('wp_insert_post_data', 'ad_change_title');
function ad_change_title($data)
    if($data['post_type'] != 'ad_listing')
        return $data;
        return $data;

    global $post;   
    update_post_meta((int)$post->ID, 'cp_entitle_englishel', (string)$_POST['qtrans_title_en']);

    return $data;

but the "update_post_meta" part. I have checked the post_id, cp_entitle_englishel, qtrans_title_en values, and they are valid. Any suggestions?


Two issues with your code in current form:

  1. wp_insert_post_data hook runs before the actual insertion happens. In other words post might not not exist yet.

  2. Data in this hook does not contain post_id.

More fitting hooks to use are those after post insertion is processed:

do_action('save_post', $post_ID, $post);
do_action('wp_insert_post', $post_ID, $post);
  • The problem is that I don't want to save the whole post, just update the custom field. "ad_change_title" function contains some other actions that I didn't mention, in order to keep the code simple and clear. – ktsixit Oct 9 '12 at 7:38
  • I also corrected the post_id thing in question's code. – ktsixit Oct 9 '12 at 7:48
  • @ktsixit whatever you want to modify in post doesn't change the fact that the hook you are trying to use is not appropriate. There simply might be no post to work with at that point. – Rarst Oct 9 '12 at 10:11
  • eventually I had to add two different hooks, one for the post_title and one for the custom field. Thank you for your suggestions – ktsixit Oct 10 '12 at 13:11

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.