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 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;
    if(!is_admin())
        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?

share|improve this question
add comment

1 Answer

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);
share|improve this answer
    
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
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.