WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

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'd like to run a custom query using some meta data whenever a post is updated or published.

Is there something I can put in functions.php to fire when those events occur?

share|improve this question
up vote 8 down vote accepted

The save_post action fires When a post is updated and/or published -- including when a new post is inserted.

add_action( 'save_post', 'wpse41912_save_post' );
function wpse41912_save_post()
    // do stuff

If you want your functions to fire only when a post is being edited, you can hook into edit_post.

If you want it to fire when a post is moved from draft to publish you can hook into transition_post_status.

share|improve this answer
save_post is for when it's created or updated. You will want publish_post for when it's actually posted. If you don't do that - then drafts and such will be impacted as well. – Sterling Hamilton Feb 10 '12 at 18:51
save_post is fired whenever wp_insert_post is called. On everything -- drafts, whatever. If you want to stop it from being called during auto drafts, you can check if( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) return somewhere in the function hooked into save_post. – chrisguitarguy Feb 10 '12 at 20:08
+1 for edit_post function – Warface Jul 2 '14 at 13:28

the edit_post hook is probably the best hook... as it fires whenever a post/page is published or updated. save_post is another viable option... maybe better if you don't need it to fire when comments are updated.


the save_post page in the codex has an example of a function that fires whenever a post is saved:


share|improve this answer

There are several actions you can use. For example:

  • save_post
  • edit_post
  • publish_post
  • etc.

While lesser-used, there are also post-status transition hooks.

share|improve this answer

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.