WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
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

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:


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

Your Answer


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.