Tim
Tim

Reputation: 145

Wordpress: executing function when saving or editing post

This is what I'm trying to accomplish: If I'm adding or editing post I want to run a function that puts post_id, all the custom field values and category ids to some other db table I've created. I just want to know how to execute this function and how to get the values.

Upvotes: 4

Views: 5493

Answers (2)

pixelDino
pixelDino

Reputation: 105

This was firing twice.

function do_my_stuff($post_ID)  {
   //do my stuff here;
   return $post_ID;
}
add_action('save_post', 'do_my_stuff');

Adding

if (wp_is_post_revision($post_ID)) return;

to suppress revision solved it for me. Maybe you want to suppress more...

// API request
if (REST_REQUEST) return;

// autosave
if (wp_is_post_autosave($post_ID)) return;

Upvotes: 0

JohnP
JohnP

Reputation: 50029

Put the following in your functions.php file of your theme. It will run on both save as well as well update. Since you have the post ID, you can do whatever you want.

function do_my_stuff($post_ID)  {
   //do my stuff here;
   return $post_ID;
}

add_action('save_post', 'do_my_stuff');

Upvotes: 5

Related Questions