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 want to in insert data in my custom db table when creating a post.

Suppose there are three categories (php, wordpress & jquery). When i create a post in wordpress, i want to insert post id and category id in my custom database table. How can i do this job?

share|improve this question

1 Answer 1

up vote 0 down vote accepted

You hook a function to save_post (for saves and updates) or publish_post for publication only. The second hook is really a variable hook of the form {$new_status}_{$post->post_type} so it will be different if you aren't dealing with a post post type.

function do_on_publish_wpse_98177($id) {
  // your code; $id is the post ID
}
add_action('publish_post','do_on_publish_wpse_98177');

Make backups and test thoroughly on a development server. It is easy to break things with these hooks.

You can use $id to pull information from the database including category information. You can also access the global $_POST if you need it but these hooks run after the post save so if something has altered (another function) the data saved to the database $_POST might be out of sync.

See also:

http://codex.wordpress.org/Post_Status_Transitions

share|improve this answer
    
save_post works for me :) Thank you –  Shakil May 6 '13 at 12:11

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.