WordPress save_post action not firing when save

Below is my code that will take the post_id and randomly get 3 posts from the same category and store them as custom field. The code is clearly working, because when I click “New Post”, I see the custom field being populated, but when I clicked “Publish” or “Save”, the code below didn’t get executed, from what I understand, Save_Post will be called once when creating new post and one more time when you actually save the post.

Funny thing is that the code below works in Local server, WAMP, but not on my production server, I don’t know why. They all use the same plugins.

function update_postmeta($post_id) {
global $post;

if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return;
 }

 unset($rand_id); 

$cat_id = get_the_category($post_id);

 $args = array(
   'showposts' => 3,
   'orderby' => 'rand',
 'cat' => $cat_id[0]->cat_ID,
);
 $my_query = new WP_Query($args);
while ($my_query->have_posts()) : $my_query->the_post();
  $rand_id = $rand_id.get_the_ID().',';
endwhile; update_post_meta($post_id, 'related_id',$rand_id);
} add_action('save_post', 'update_postmeta');

Related posts

Leave a Reply

1 comment