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?


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.

| 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

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.

| improve this answer | |

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:


| improve this answer | |

If you want to perform an action when any custom post update/save -

function save_post_callback($post_id){
global $post; 
if ($post->post_type != 'MY_CUSTOM_POST_TYPE_NAME'){
//if you get here then it's your post type so do your things....
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.