drcrow
drcrow

Reputation: 391

Update meta field after post save/update on wordpress

I have a custom post type called "professional". After a Professional is saved (new or update) I need to override the value of a meta field called custom_permalink.

If I use this:

function afterPostUpdated($meta_id, $post_id, $meta_key='', $meta_value=''){
    if(get_post_type($post_id) == 'professional' && $meta_key=='custom_permalink') {
        if($_GET['message']==1) {
            die($meta_value);
        }
    }
}
add_action('updated_post_meta', 'afterPostUpdated', 10, 4);

The die() only occurs when the field is changed. I need this to work everytime the post is saved.

Upvotes: 1

Views: 1129

Answers (1)

drcrow
drcrow

Reputation: 391

OK, I found my aswer:

function afterPostUpdated($meta_id, $post_id, $meta_key='', $meta_value=''){
    if(get_post_type($post_id) == 'tab_especialidad') {
        if($_GET['message']==1) {
            $terms = get_the_terms($post_id, 'especialidades');
            $esp_slug = $terms[0]->slug;
            $post_slug = sanitize_title(get_the_title($post_id));
            $custom_permalink = 'especialidades/'.$esp_slug.'/'.$post_slug.'/';
            //die($custom_permalink);
            update_post_meta($post_id, 'custom_permalink', $custom_permalink);
        }
    }
}
add_action('updated_post_meta', 'afterPostUpdated', 10, 4);

Upvotes: 1

Related Questions